2014-12-03 82 views
1

假设我有下面的类,这是一个简单的类,用于将三个字符串添加到名为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()); 
} 

} 
+0

创建一个数组containsin g要测试的值,然后遍历每个值并检查返回的ArrayList具有与此数组相同的长度和值。 – 2014-12-03 20:49:09

+0

嘿!我在OP中添加了一些代码。我创建了新的1D阵列并填充它。我只是不确定如何比较ArrayList和Array。 – 2014-12-03 21:04:27

回答

2

下面的代码应该做的:

@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)); 
} 
+2

我喜欢janos的答案更好;) – realUser404 2014-12-03 21:05:55

+1

尼斯体育精神;-) – janos 2014-12-03 21:07:24

+0

我得到一个无法找到符号类TestCases,位置TestCasesTes“,我想知道为什么会这样,我的主要方法类是Testcases(小写c),但即使这样,我也会得到相同的错误 – 2014-12-03 21:08:48

3

在这里你去:

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()); 
    } 
} 
01在返回类型,每当变量声明可能

  • 使用的接口类型:

    我做了你的方法了一些改进。所以,我在返回类型和局部变量

  • 无需在return(ar)的括号改变ArrayListList,这是简单和自然:return ar
+0

是不是很好的做法,相同的课程? – realUser404 2014-12-03 21:07:39

+0

绝对不是,但他的班级名为“Testcases”,所以它看起来实际上就是他的测试班级,组织测试用例在这里是可疑的,没有足够的信息来评论它,所以我 – janos 2014-12-03 21:09:59

+0

我得到一个错误,在我的TestcasesTest文件中找不到符号myArray(),这是在哪里测试完成。测试用例,主要方法 - 虽然很好。我已经添加了TestcasesTest到OP。 – 2014-12-03 21:12:30

0

考虑使用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"); 
    } 

你将以这种方式获得非常具有描述性的错误失败消息:

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]>