Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Mockito's verify() counts in when() calls?

The following test fails with org.mockito.exceptions.verification.TooManyActualInvocations for no apparent reason.
Mockito logs point out 2 invocations: first is the one I expect and the second one is in setUp() method's when() call.
I do not recall Mockito counting in when() calls, it makes zero sense. Does this have to do with Answers.RETURNS_DEEP_STUBS or am I missing something else?

@RunWith(MockitoJUnitRunner.class)
public class MyTest {

    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
    private FooService mockedObject;

    void setUp() {
        when(mockedObject.putParameter(any()).firstChainCall().secondChainCall()).thenReturn(HttpStatus.SC_OK);
    }

    @Test
    public void foo() throws Exception {
        runStuff();
        verify(mockedObject).putParameter(any());
    }
}
like image 859
miracle_the_V Avatar asked Jan 27 '26 19:01

miracle_the_V


1 Answers

It's counting your setup as invocations since deeps stubs is not supported in the verification API, and complains on the second call

like image 115
anish sharma Avatar answered Jan 29 '26 10:01

anish sharma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!