-3
SubList(Java.util.List)本地实现的Im测试方法。我使用Java 8 api文档。对于toArray()方法,我只有3个测试。你能帮助我想出更多的测试用例吗?由于SubList测试用例
@Test
public void toArrayBasicSub() {
fillList();
List<String> expectedList = Arrays.asList("1","2","3","4","5");
String[] expected = {"1","2","3","4","5"};
List<String> sub = list.subList(0, 5);
Object[] actual = sub.toArray();
assertEquals(expectedList,list);
assertArrayEquals(expected,actual);
}
@Test
public void toArrayEmpty() {
String[] expected = {};
List<String> sub = list.subList(0, 0);
Object[] actual = sub.toArray();
assertArrayEquals(expected,actual);
}
@Test
public void toArrayNull() {
fillList();
List<String> expected = null;
List<String> sub = list.subList(0, 5);
sub = null;
boolean thrownException = false;
try {
sub.toArray();
} catch (NullPointerException e) {
thrownException = true;
}
assertTrue(thrownException);
}
为什么要测试JDK的方法?这已经由开发它的团队测试过了。你应该测试你自己的代码。 – Beggs
并且不要使用try-catch来测试异常。可以使用'@Test(expected = NullPointerException.class)'或ExpectedException测试规则。 –