如何使用嵌套for循环测试方法

时间:2012-09-17 12:20:14

标签: java unit-testing junit mocking mockito

我是单位测试的初学者。 我使用JUnit和Mockito。这是我想测试的一个示例方法。

public List<Person> getPeopleList(List<Aggregate<Person>> aggregateList) {
    List<Person> resultList = new ArrayList<Person>();
    for (Aggregate<Person> aggregate : aggregateList) {
        resultList.add(aggregate);

        for (Person person : aggregate) {
            resultList.add(person);
        }
    }
    return resultList; // the result is person and aggregate list
}

我尝试了很多方法,但我做得不好。例如:

@Test
public void getPeopleListTest(){
    ClassUnderTest testedClass = new ClassUnderTest();

    Aggregate aggregate = mock(Aggregate.class);
    Iterator<Aggregate<Person>> aggregateIterator = mock(Iterator.class);
    when(aggregateIterator.hasNext()).thenReturn(true, false);
    when(aggregateIterator.next()).thenReturn(aggregate);

    List<Aggregate<Person>> aggregateList = mock(List.class);

    aggregateList.add(aggregate);

    List<Person> list = testedClass.getPeopleList(aggregateList);

    assertEquals(1, list.size());
}

提前谢谢。

2 个答案:

答案 0 :(得分:3)

我不会嘲笑所有可能的事情。我只会模拟你想测试的类,并假设List行为正确。

public class Main {
    interface Person {
    }

    interface Aggregate<T> extends Person, Iterable<T> {
    }

    public static List<Person> getPeopleList(List<Aggregate<Person>> aggregateList) {
        List<Person> resultList = new ArrayList<Person>();
        for (Aggregate<Person> aggregate : aggregateList) {
            resultList.add(aggregate);

            for (Person person : aggregate) {
                resultList.add(person);
            }
        }
        return resultList; // the result is person and aggregate list
    }

    public static void main(String... args) {
        Aggregate<Person> aggregate = mock(Aggregate.class);
        Aggregate<Person> aggregate2 = mock(Aggregate.class);
        Person person = mock(Person.class);
        Person person2 = mock(Person.class);
        when(aggregate.iterator()).thenReturn(Arrays.asList(person).iterator());
        when(aggregate2.iterator()).thenReturn(Arrays.asList(person2).iterator());

        List<Person> list = getPeopleList(
                Arrays.asList(aggregate, aggregate2));
        System.out.println(list);
        System.out.println("size: " + list.size());
    }
}

打印

[Mock for Aggregate, hashCode: 2037567902, Mock for Person, hashCode: 1629493852, Mock for Aggregate, hashCode: 44220373, Mock for Person, hashCode: 182467149]
size: 4

答案 1 :(得分:3)

你需要问问自己,你试图测试的是什么?在示例函数中,您要检查当函数被赋予聚合列表时,它会返回聚合和人员的混合扁平列表。

所以,我不会模拟List,而是检查结果列表是否包含所有预期的项目:

Aggregate aggregate = new Aggregate(...);
//add persons to aggregate.

List<Aggregate<Person>> aggregateList = new ArrayList();
aggregateList.add(aggregate);

List<Person> list = testedClass.getPeopleList(aggregateList);
assertEquals(Collections.singletonList(aggregate), result);

否则,如果您更改迭代参数列表中所有项目的方式,则需要更新测试。

你可以测试的另一件事:

  1. 不修改作为参数给出的列表。
  2. 函数检查null参数。

    Preconditions.checkNotNull(aggregateList, "AggregateList cannot be null");
    
  3. 空参数导致空结果。