`verifyNoMoreInteractions()` and `verifyZeroInteractions()` are two methods in Mockito used for verifying interactions with mock objects, but they serve slightly different purposes, although their behavior is similar in certain versions of Mockito.
`verifyNoMoreInteractions()`
This method is used to ensure that no additional interactions occur on a mock object beyond what has already been verified using `verify()`. It should be called after all other `verify()` calls to confirm that there are no unverified interactions. If any unverified interactions are found, the test will fail. This method is particularly useful for ensuring that only the expected methods are called on a mock object during a test.
For example, if you have a mock object `mockContext` and you want to verify that only the `addError()` method is called on it, you would use `verifyNoMoreInteractions()` like this:
java
Context mockContext = mock(Context.class);
Validator validator = new Validator(mockContext);
validator.validate("Hello world");
verify(mockContext).addError("No space allowed.");
verifyNoMoreInteractions(mockContext);
`verifyZeroInteractions()`
This method checks that no interactions at all have occurred on a mock object. It is similar to `verifyNoMoreInteractions()` in that it ensures no interactions are present, but it does not require prior `verify()` calls. It is typically used when you want to ensure that a mock object is not used at all during a test.
In Mockito versions prior to 3.x, `verifyZeroInteractions()` behaves the same as `verifyNoMoreInteractions()`, but in newer versions, the behavior might differ slightly, with `verifyZeroInteractions()` specifically checking for no interactions whatsoever.
Here's how you might use `verifyZeroInteractions()` to ensure that a mock object `setter` is not interacted with:
java
verifyZeroInteractions(setter);
Key Differences
- Purpose: `verifyNoMoreInteractions()` is used after verifying specific interactions to ensure no additional interactions occur, while `verifyZeroInteractions()` checks that there are no interactions at all.
- Usage Context: `verifyNoMoreInteractions()` is typically used after other `verify()` calls, whereas `verifyZeroInteractions()` can be used independently.
- Behavior in Different Versions: In Mockito versions prior to 3.x, both methods behave similarly, but in newer versions, `verifyZeroInteractions()` might be more strictly about ensuring no interactions.
In summary, while both methods can be used to ensure that mock objects are not over-interacted with, `verifyNoMoreInteractions()` is more about ensuring that only the expected interactions have occurred, whereas `verifyZeroInteractions()` is about ensuring no interactions at all.
Citations:[1] https://stackoverflow.com/questions/59702751/what-is-the-downfall-of-using-verifynomoreinteractions-in-each-test
[2] https://www.lambdatest.com/automation-testing-advisor/selenium/methods/org.mockito.Mockito.verifyZeroInteractions
[3] https://www.centron.de/en/tutorial/mockito-verify-tutorial/
[4] https://mincong.io/2019/09/22/mockito-verify/
[5] https://stackoverflow.com/questions/11936301/mockito-how-to-verify-that-a-mock-was-never-invoked
[6] https://stackoverflow.com/questions/52396683/difference-between-verifynomoreinteractions-and-verifyzerointeractions-in-mockit
[7] https://www.digitalocean.com/community/tutorials/mockito-verify
[8] https://objectcomputing.com/resources/publications/sett/september-2010-mockito-integration-testing-made-easier
[9] https://www.lambdatest.com/automation-testing-advisor/selenium/methods/org.mockito.Mockito.verifyNoInteractions
[10] https://groups.google.com/g/mockito/c/qq_94rFJyoc
[11] https://docs.openrewrite.org/recipes/java/testing/mockito/verifyzerotonomoreinteractions