The `verifyNoMoreInteractions` method in Mockito is used to ensure that no further interactions occur on a mock object after a certain point in the test. When used in conjunction with `InOrder`, it provides a powerful way to verify not only that interactions happen in a specific order but also that no additional interactions occur after the specified sequence.
How `InOrder` Works
`InOrder` is a Mockito feature that allows you to verify that interactions with mock objects occur in a specific order. It is particularly useful when you need to ensure that certain methods are called before others. Here's an example:
java
// Create mock objects
List firstMock = Mockito.mock(List.class);
List secondMock = Mockito.mock(List.class);
// Interact with the mocks
firstMock.add("A");
secondMock.add("B");
// Create an InOrder object
InOrder inOrder = Mockito.inOrder(firstMock, secondMock);
// Verify interactions in order
inOrder.verify(firstMock).add("A");
inOrder.verify(secondMock).add("B");
Using `verifyNoMoreInteractions` with `InOrder`
After verifying the interactions in order, you can use `verifyNoMoreInteractions` to ensure that no additional interactions occur on the mock objects. This method checks that there are no more invocations on the mocks after the last verified interaction.
Here's how you can use it:
java
// After verifying interactions in order
inOrder.verifyNoMoreInteractions();
This call will fail if there are any additional interactions on the mock objects after the last verified interaction, ensuring that your test is strict about the sequence and number of interactions.
Example Scenario
Consider a scenario where you want to ensure that a list's `add` method is called with "A" and then "B", and no other interactions occur:
java
List listMock = Mockito.mock(List.class);
listMock.add("A");
listMock.add("B");
InOrder inOrder = Mockito.inOrder(listMock);
inOrder.verify(listMock).add("A");
inOrder.verify(listMock).add("B");
inOrder.verifyNoMoreInteractions();
If any additional interactions occur on `listMock` after adding "B", the test will fail, providing a clear indication that the expected behavior was not met.
Key Points
- Order Matters: `InOrder` ensures that interactions occur in a specific sequence.
- No Additional Interactions: `verifyNoMoreInteractions` checks that no further interactions occur after the last verified interaction.
- Strict Verification: Combining both methods provides strict control over the interactions with mock objects, ensuring that tests are robust and accurate.
By using `verifyNoMoreInteractions` in conjunction with `InOrder`, you can write tests that are both order-sensitive and strict about the number of interactions, making your test suite more reliable and comprehensive.
Citations:[1] https://javadoc.io/static/org.mockito/mockito-core/2.2.7/org/mockito/InOrder.html
[2] https://site.mockito.org/javadoc/current/org/mockito/Mockito.html
[3] https://mincong.io/2019/09/22/mockito-verify/
[4] https://www.javacodegeeks.com/2015/11/mockito-verification.html
[5] https://stackoverflow.com/questions/55357899/mockito-inorder-verify-does-not-prohibit-intermediate-calls-on-a-mock
[6] https://www.lambdatest.com/automation-testing-advisor/selenium/methods/org.mockito.Mockito.verifyNoInteractions
[7] https://www.digitalocean.com/community/tutorials/mockito-verify
[8] https://groups.google.com/g/mockito/c/qq_94rFJyoc
[9] https://www.lambdatest.com/automation-testing-advisor/selenium/methods/org.mockito.internal.MockitoCore.inOrder