Java 在ArrayList上进行Junit测试
假设我有下面的类,一个简单的类,只需将三个字符串添加到名为ar的字符串ArrayList中Java 在ArrayList上进行Junit测试,java,arrays,testing,arraylist,junit,Java,Arrays,Testing,Arraylist,Junit,假设我有下面的类,一个简单的类,只需将三个字符串添加到名为ar的字符串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()
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);
}
}
以下代码应执行以下操作:
@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));
}
@测试
公共无效myArrayTest(){
TestCases TestCases=新的TestCases();
List result=testCases.myArray();
Assert.assertNotNull(“列表不应为null”,结果);
Assert.assertEquals(“错误的大小”,3,result.size());
Assert.assertEquals(“错误的第一个元素”,“Customer1”,result.get(0));
Assert.assertEquals(“错误的第二个元素”,“Customer2”,result.get(1));
Assert.assertEquals(“错误的第三个元素”,“Customer3”,result.get(2));
}
给你:
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());
}
}
公共类测试用例{
公共列表myArray(){
List ar=new ArrayList();
ar.add(“客户1”);
ar.add(“客户2”);
ar.add(“客户3”);
返回ar;
}
}
类TestcasesTest{
@试验
公共void testMyArray(){
Testcases Testcases=新的Testcases();
assertEquals(Arrays.asList(“Customer1”、“Customer2”、“Customer3”)、testcases.myArray();
}
}
我对你的方法做了一些改进:
- 尽可能在返回类型和变量声明中使用接口类型。因此,我将返回类型和局部变量中的
更改为ArrayList
List
- 无需使用
中的参数,这更简单、更自然:return(ar)
return ar
import static org.assertj.core.api.Assertions.assertThat;
@Test
public void test() {
final List<String> result = classUnderTest.someMethod();
assertThat(result).containsExactly("Customer1", "Customer2", "Customer3");
}
import static org.assertj.core.api.Assertions.assertThat;
@试验
公开无效测试(){
最终列表结果=classUnderTest.someMethod();
资产(结果)。实际包含(“客户1”、“客户2”、“客户3”);
}
您将通过以下方式获得非常描述性的错误失败消息:
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]>
java.lang.AssertionError:
期望:
完全包含(并按相同顺序):
但未找到某些元素:
其他人则没有预料到:
创建一个包含要测试的值的数组,然后检查每个值并检查返回的ArrayList
是否具有与此数组相同的长度和值。嘿!我在OP中添加了一些代码。我创建了新的1D数组并填充了它。我只是不知道如何比较ArrayList和Array。我更喜欢janos的答案;)我得到一个找不到的符号类TestCases,location TestCasesTes“。我想知道为什么会这样。我的主要方法是TestCases(小写c)但即便如此,我还是遇到了同样的错误。你在哪里编写了测试?也许你只需要导入类。很抱歉键入错误的Testcases,但CamelCase说应该是Testcases:)绝对不是。但他的类被称为“Testcases”,所以看起来这实际上是他的测试类。测试用例的组织在这里是可疑的,没有足够的信息来评论它,所以我很方便地避开了这个潜在的问题。我遇到了一个错误,在我的TestcasesTest文件中找不到symbol myArray(),测试就是在这里完成的。不过,主要方法Testcase很好。我已将TestcasesTest添加到OP中。我相应地更新了我的帖子,使用了与您相同的主类和测试类名称。
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]>