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



我是单元测试的新手。我使用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());
}

提前谢谢你。

我不会嘲笑所有可能的事情。我只会模拟您要测试的类并假设 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

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

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

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. 空参数会导致空结果。

相关内容

  • 没有找到相关文章