单元测试一个转换为集合的数组

huangapple go评论68阅读模式
英文:

Unit testing an array converted set

问题

我有一个数组。我需要将其转换为集合然后进行单元测试。我成功地将数组转换为集合。但是单元测试不起作用。我是一个新的学习者,特别是在单元测试方面。请帮助。

public class ArrToSet {
    public static void main(String[] args) {
        String[] array = {"a", "b", "c"};
        System.out.println("Original Array: " + Arrays.toString(array));
        System.out.println("Array converted to Set: " + Arrays.stream(array).collect(Collectors.toSet()));
    }
}

我编写的单元测试案例:

class ArrToSetTest {
    @Test
    void test() {
        ArrToSet ar = new ArrToSet();
        Set<String> set = new HashSet<>();
        set.add("a");
        set.add("b");
        set.add("c");
        String actual[] = {"a", "b", "c"};
        assertEquals(set, actual);
    }
}
英文:

I have an array. I am required to convert it into set and then unit test. I successfully did the array to set conversation. But the unit test is not working. I am a new learner specially in unit testing. Please help.

public class ArrToSet {

    public static void main(String[] args) {
	    String[] array = {&quot;a&quot;,&quot;b&quot;,&quot;c&quot;};
	    System.out.println(&quot;Original Array: &quot; + Arrays.toString(array));
	
	    System.out.println(&quot;Array converted to Set: &quot; + Arrays.stream(array).collect(Collectors.toSet()));
	
    }
}

The unit test case I have written:

class ArrToSetTest {
    @Test
    void test() {

	    ArrToSet ar = new ArrToSet();
	
	     Set&lt;String&gt; set = new HashSet&lt;&gt;();
            set.add(&quot;a&quot;);
            set.add(&quot;b&quot;);
            set.add(&quot;c&quot;);

	    String actual[] = {&quot;a&quot;,&quot;b&quot;,&quot;c&quot;};
	
	    assertEquals(set,actual);
   }
}

答案1

得分: 2

你无法单元测试一个通过 main 方法公开其 API 的类,除非采用丑陋的黑客方法。如果将 ArrToSet 重构为公开有意义的 API,测试将变得更容易:

public class ArrToSet {
  public Set<String> convert(String[] array) {
    return Arrays.stream(array).collect(Collectors.toSet());
  }
}

现在可以轻松进行测试:

@Test
void test() {
  ArrToSet arrToSet = new ArrToSet();    
  Set<String> set = new HashSet<>();
  set.add("a");
  set.add("b");
  set.add("c");

  String actual[] = {"a", "b", "c"};
    
  assertEquals(set, arrToSet.convert(actual));
}
英文:

You won't able to unit test a class that exposes its API through a main method without ugly hacks. If you refactor ArrToSet to expose meaningful API it'll be much easier to test:

public class ArrToSet {
  public Set&lt;String&gt; convert(String[] array) {
    return Arrays.stream(array).collect(Collectors.toSet()));
  }

  public static void main(String[] args) {
    String[] array = {&quot;a&quot;,&quot;b&quot;,&quot;c&quot;};
    System.out.println(&quot;Original Array: &quot; + Arrays.toString(array));
    ArrToSet arrToSet = ArrToSet();
    System.out.println(&quot;Array converted to Set: &quot; + arrToSet.convert(array));
  }
}

Testing this is easy now:

@Test
void test() {
  ArrToSet ar = new ArrToSet();    
  Set&lt;String&gt; set = new HashSet&lt;&gt;();
  set.add(&quot;a&quot;);
  set.add(&quot;b&quot;);
  set.add(&quot;c&quot;);

  String actual[] = {&quot;a&quot;,&quot;b&quot;,&quot;c&quot;};
    
  assertEquals(set, arrToSet.convert(actual));
}

huangapple
  • 本文由 发表于 2020年7月22日 01:07:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63019542.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定