Consider the following badly designed code:
public interface BadPrecedentService {
void setThePassedInList(List items);
}
This is being used as below:
public class BadPrecedentClient {
private final BadPrecedentService badPrecedentService;
public BadPrecedentClient(BadPrecedentService badPrecedentService) {
this.badPrecedentService = badPrecedentService;
}
public List getNamesOfActiveItems() {
List items = new ArrayList(10);
badPrecedentService.setThePassedInList(items);
List names = new ArrayList(items.size());
for (Item item : items) {
if (item.isActive()) {
names.add(item.getName());
}
}
return names;
}
}
How can I test this method with Mockito? Look at the following snippet:
public class BadPrecedentClientTest {
@Test
public void testGetNamesOfActiveItems() {
// given
BadPrecedentService badPrecedentService = mock(BadPrecedentService.class);
doAnswer(new Answer
The code can be found here: https://github.com/paawak/blog/tree/master/code/MockitoExample