Java ArrayList 上的 Junit 测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27281306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Junit testing on an ArrayList
提问by Adam Horrigan
Suppose that I have the below class, a simple class just to add three Strings into a String ArrayList named ar.
假设我有下面的类,一个简单的类只是将三个字符串添加到名为 ar 的 String ArrayList 中。
public class Testcases {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
}
public ArrayList<String> myArray() {
ArrayList<String> ar = new ArrayList<String>();
ar.add("Customer1");
ar.add("Customer2");
ar.add("Customer3");
return(ar);
}
}
How could I use Junit testing to make sure that the Strings actually went into ArrayList?
我如何使用 Junit 测试来确保字符串确实进入了 ArrayList?
Update
更新
My TestcasesTest file - where the testing is done, looks as follows:
我的 TestcasesTest 文件 - 测试完成的地方,如下所示:
@Test
public void testMain() {
System.out.println("main");
String[] args = null;
Testcases.main(args);
// TODO review the generated test code and remove the default call to fail.
// fail("the test case is a resul in the prototype");
}
/**
* Test of add method, of class Testcases.
*/
@Test
public void testMyArray() {
assertEquals(Arrays.asList("Customer1", "Customer2", "Customer3"), myArray());
}
}
回答by realUser404
Following code should do :
以下代码应该做:
@Test
public void myArrayTest() {
TestCases testCases = new TestCases();
List<String> result = testCases.myArray();
Assert.assertNotNull("List shouldn't be null", result);
Assert.assertEquals("wrong size", 3, result.size());
Assert.assertEquals("Wrong 1st element", "Customer1", result.get(0));
Assert.assertEquals("Wrong 2nd element", "Customer2", result.get(1));
Assert.assertEquals("Wrong 3rd element", "Customer3", result.get(2));
}
回答by janos
Here you go:
干得好:
public class Testcases {
public List<String> myArray() {
List<String> ar = new ArrayList<>();
ar.add("Customer1");
ar.add("Customer2");
ar.add("Customer3");
return ar;
}
}
class TestcasesTest {
@Test
public void testMyArray() {
Testcases testcases = new Testcases();
assertEquals(Arrays.asList("Customer1", "Customer2", "Customer3"), testcases.myArray());
}
}
I made some improvements on your method:
我对你的方法做了一些改进:
- Use interface types in return type and variable declarations whenever possible. So I changed
ArrayList
toList
in the return type and the local variable - No need for the parens in
return(ar)
, this is simpler and natural:return ar
- 尽可能在返回类型和变量声明中使用接口类型。所以我改变
ArrayList
,以List
在返回类型和局部变量 - 不需要括号
return(ar)
,这更简单自然:return ar
回答by Andreas
Consider using Assert4J:
考虑使用 Assert4J:
import static org.assertj.core.api.Assertions.assertThat;
@Test
public void test() {
final List<String> result = classUnderTest.someMethod();
assertThat(result).containsExactly("Customer1", "Customer2", "Customer3");
}
You'll get very descriptive error failure messages this way:
您将通过这种方式获得非常具有描述性的错误失败消息:
java.lang.AssertionError:
Expecting:
<[5544, 8811, 9988]>
to contain exactly (and in same order):
<["Customer1", "Customer2", "Customer3"]>
but some elements were not found:
<["Customer1", "Customer2", "Customer3"]>
and others were not expected:
<[9988, 8811, 5544]>