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.