I am beginer in unit testing.
I use JUnit and Mockito. This is one example method which I want to test.
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
}
I tried to many ways, but I can’t do it well. Example:
@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());
}
Thank you in advance.
I wouldn’t mock every possible thing. I would only mock the class you want to test and assume List behaves correctly.
prints