Can't Tell Can't Tell - 7 days ago 6
Java Question

How to use ArgumentCaptor for stubbing?

In mockito documentation and javadocs it says


it is recommended to use ArgumentCaptor with verification but not
with stubbing


but I dont't unserstand how ArgumentCaptor can be used for stubbing. Can someone explain the above statement and show how ArgumentCaptor can be used for stubbing or provide a link that shows how it can be done?

Answer

Assume that you have the following method:

public boolean doSomething(SomeClass arg);

Mockito documentation says that you should not use captor in this way:

ArgumentCaptor<SomeClass> argumentCaptor = ArgumentCaptor.forClass(SomeClass.class);
when(someObject.doSomething(argumentCaptor.capture())).thenReturn(true);

// invoke SUT

SomeClass expected = // some expected value
assertThat(argumentCaptor.getValue(), equalTo(expected))

The reason is simple - you can just use matcher during stubbing:

SomeClass expected = // some expected value
when(someObject.doSomething(eq(expected))).thenReturn(true);

// invoke SUT

But verification may be the different story. Let say, you do not need to stub doSomething. You are quite happy with default return value from mock (it will be false). So you don't need to stub. But your test needs to ensure that this method was called and its argument was correct. Then you use ArgumentCaptor and this is the case for which it is designed:

// invoke SUT

ArgumentCaptor<SomeClass> argumentCaptor = ArgumentCaptor.forClass(SomeClass.class);
verify(someObject).doSomething(argumentCaptor.capture());
SomeClass expected = // some expected value

assertThat(argumentCaptor.getValue(), equalTo(expected))
Comments