In Mockito we have a situation where the capture of a list doesn't return the expected result. Test case:
In our assert we only expect "Pip" to be there but "Sok" is there too. We think this is incorrect because at the time of capture "Sok" was not in the list.
java.lang.AssertionError:
Expected :[Pip]
Actual :[Pip, Sok]
Here is the test case:
@RunWith(MockitoJUnitRunner.class)
public class CaptureTest {
@Captor
private ArgumentCaptor<List> listCapture;
@Mock
private ListPrinter listPrinter;
private TestClass testClass;
@Before
public void setUp() {
testClass = new TestClass(listPrinter);
}
@Test
public void testCapture() {
testClass.simulateFailSituation();
verify(listPrinter).printList(listCapture.capture());
// THIS FAILS: Expected:[Pip], Actual:[Pip, Sok]
assertEquals(Collections.singletonList("Pip"), listCapture.getValue());
}
public class TestClass {
private List list = new ArrayList();
private ListPrinter listPrinter;
public TestClass(ListPrinter listPrinter) {
this.listPrinter = listPrinter;
}
private void simulateFailSituation() {
list.add("Pip");
listPrinter.printList(list);
list.add("Sok");
}
}
public interface ListPrinter {
void printList(List list);
}
}
It might sound like an amazing feature, but then think about it that way: If it was making a copy, where should it stop? You could possibly capture some object that has many references to other objects and you might end up making a deep copy of nearly all the objects in your JVM instance.
That would be a serious performance hit, so I kinda understand why.
So, there are two approaches you could choose from:
doAnswer
method with Answer<Void>
, test it there or make a copy. By the way this is noting new, see How to make mock to void methods with mockito.I find it a lot more powerful than verify. In your case, the doAnswer
could look like that:
doAnswer(invocation -> {
assertEquals(Collections.singletonList("Pip"), invocation.getArguments()[0]);
return null;
}).when(listPrinter).printList(Matchers.anyList());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With