我是单元测试的初学者。我使用 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());
}
先感谢您。