Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

verifyNoMoreInteractions does not work as expected when used with MockitoSession #2184

Open
devstdout opened this issue Jan 21, 2021 · 1 comment

Comments

@devstdout
Copy link

I'm seeing an issue with Mockito.verifyNoMoreInteractions when used with MockitoSession.

In the example below, all tests should fail as we have an interaction with supplier1 that is not taken into account (verify(supplier1).getAsInt() is commented).

However testWithMockitoSession passes. It only exhibit this behaviour when a method on the mocked object is stubbed (when(supplier1.getAsInt()).thenReturn(100)).

public class BuggyMockito {

    @Mock
    private IntSupplier supplier1;
    @Mock
    private IntSupplier supplier2;

    @Test
    public void testNormalMock() {
        testTheThing(mock(IntSupplier.class), mock(IntSupplier.class));
    }

    @Test
    public void testInitMocks() {
        MockitoAnnotations.initMocks(this);
        testTheThing(supplier1, supplier2);
    }

    @Test
    public void testWithMockitoSession() {
        final MockitoSession mockitoSession = Mockito.mockitoSession().initMocks(this).startMocking();
        testTheThing(supplier1, supplier2);
        mockitoSession.finishMocking();
    }

    private static void testTheThing(final IntSupplier supplier1, final IntSupplier supplier2) {
        when(supplier1.getAsInt()).thenReturn(100);

        supplier1.getAsInt();
        supplier2.getAsInt();

        //verify(supplier1).getAsInt();
        verify(supplier2).getAsInt();

        verifyNoMoreInteractions(supplier1, supplier2);
    }
}

This test was done on openjdk version "11.0.9.1" 2020-11-04 with Mockito 3.7.7.

@jimwrightcz
Copy link

verifyNoMoreInteractions() does not seem to work for me when a method is stubbed. I don't use MockitoSession. I have:

when(myMock.myMethod(...)).thenReturn(...);
doSomething();
verify(myMock, times(1)).myMethod(...);
verifyNoMoreInteractions();

which passes but I expect it to fail if I remove the first verify line and it does not. A similar test without when(...) fails on the last line if the method call is not verified.

I am using openjdk 17.0.5 and Mockito 4.8.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants