0

I'm trying to Test a Method where I call a method from three different other Classes (adapterX, adapterY, adapterZ). I want to verify that only one of these Methods is getting executed with the verify(...) method from Mockito.

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {MessageValidationImplTest.TestConfig.class})
class MessageValidationImplTest {

    @TestConfiguration
    public static class TestConfig {
        // Creation of other Beans messageValidation depends on
    }

    @MockBean
    private AdapterY adapterY;

    @MockBean
    private AdapterX adapterX;

    @MockBean
    private AdapterZ adapterZ;

    @Test
    void testJsonPayload() {
        // Arrange
        //...

        //Act
        this.messageValidation.messageValidation(message, parameter);

        //Assert
        verify(this.adapterX).sendMessage(messageDto);
        verifyNoInteractions(this.adapterY);
        verifyNoInteractions(this.adapterZ);
    }

    @Test
    void testCsvPayload() {
        // Arrange
        // ...

        //Act
        this.messageValidation.messageValidation(message, parameter);

        //Assert
        verifyNoInteractions(this.adapterX);
        verify(this.adapterY).sendMessage(messageDto);
        verifyNoInteractions(this.adapterZ);
    }
}

I am creating the mocks via @MockBean from Spring.
When running the Tests I get this error message in the Logs (I get the same Error for the other Adapter in the second Test):

org.mockito.exceptions.verification.NoInteractionsWanted: 
No interactions wanted here:
-> at com.acme.example.moduletest.core.impl.MessageValidationImplTest.testJsonPayload(MessageValidationImplTest.java:155)
But found these interactions on mock 'com.acme.example.y.AdapterY#0 bean':
-> at com.acme.example.core.impl.MessageValidationImpl.validateMessage(MessageValidationImpl.java:61)
Actually, above is the only interaction with this mock.

I've already tried manually resetting the Mocks with clearInvocations(adapterX, adapterY, adapterZ) but that doesn't make any difference.

stuchlyf
  • 187
  • 1
  • 2
  • 11
  • JUnit 4 or 5? You should look at using `@ExtendWith` for JUnit 5. See https://stackoverflow.com/a/40962941/639520 – E-Riz Jan 17 '23 at 14:54
  • Does this answer your question? [How to use Mockito with JUnit5](https://stackoverflow.com/questions/40961057/how-to-use-mockito-with-junit5) – E-Riz Jan 17 '23 at 14:58
  • 1
    And why is this error wrong? Looks like your assertions are wrong, you expect no calls but there is a call. Looks like your test is wrong or your implementation. – M. Deinum Jan 17 '23 at 14:59
  • @E-Riz , I've just Updated the Code and made it a bit more specific. I'm annotating the class with `@ExtendWith(SpringExtension.class)`, as far as I know this should already include everything for Mockito – stuchlyf Jan 17 '23 at 15:14
  • @M.Deinum , I've already verified, that the Methods in the Errors aren't being called with the debugger. Also when running one Test by itself it work, both of them are successful, they only fail when running the whole class. – stuchlyf Jan 17 '23 at 15:16
  • Then you must be doing weird things in your test. Please add something we can run include dependencies and your configuration. – M. Deinum Jan 17 '23 at 15:19

0 Answers0