SubList测试用例

时间:2016-09-11 13:01:31

标签: java testing junit sublist

我测试SubList(Java.util.List)本机实现的方法。我正在使用Java 8 api文档。对于toArray()方法,我只有3个测试。你能帮我提出更多的测试用例吗?谢谢

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

1 个答案:

答案 0 :(得分:0)

我假设您的目标是List.subList()仅用于学习目的(因为没有其他充分的理由来测试JDK方法!)。

所以,你基本上都在寻找反馈...所以请帮忙。

首先:明确关于您正在测试的范围。你说你想测试 subList()。如果是这样,那么您的测试应关于子列表。

然后,合理测试的数量非常短:

  1. 列表
  2. 上测试调用subList的结果
  3. 测试在非空列表上调用subList的结果(简单情况:在subList中显示一个元素)
  4. 测试在非空列表上调用subList的结果(复杂的情况:在subList中显示几个元素)
  5. 你不需要太多其他东西。具体来说:当你的工作是测试 subList()调用时;那么你不需要 toArray 的任何测试用例。

    在List上调用subList(),并返回List。这是唯一重要的事情。之后做的是通过调用subList()创建的列表...不在测试subList()的范围内!