假设我有下面的类,这是一个简单的类,用于将三个字符串添加到名为ar的字符串ArrayList中。对ArrayList的Junit测试
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);
}
}
我该如何使用Junit测试来确保字符串真正进入ArrayList?
更新
我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());
}
}
创建一个数组containsin g要测试的值,然后遍历每个值并检查返回的ArrayList具有与此数组相同的长度和值。 – 2014-12-03 20:49:09
嘿!我在OP中添加了一些代码。我创建了新的1D阵列并填充它。我只是不确定如何比较ArrayList和Array。 – 2014-12-03 21:04:27