How to verify that a specific method was not called using Mockito?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When using Mockito, a popular mocking framework for unit tests in Java, it's essential to verify the behavior of your mock objects. Sometimes, you need to ensure a method was never called, which is crucial for tests where non-interaction with certain components signifies correct behavior.
Understanding verifyNoInteractions and verifyNoMoreInteractions
Mockito provides several methods to verify that no interactions occurred on a mock. The two most commonly used are:
verifyNoInteractions(Object... mocks)- Ensures that no methods were called on the mock(s).
verifyNoMoreInteractions(Object... mocks)- Checks that no interactions happened beyond what has already been verified.
These methods are helpful in different scenarios. For instance, verifyNoInteractions is useful when a mock should not be used at all during the test, while verifyNoMoreInteractions is best when some interactions are allowed or expected, and you want to ensure there are no additional, unintended interactions after certain points in your test.
Using verify(mock, times(0))
For verifying that a specific method was not called, Mockito's verify method paired with times(0) is typically used. This approach is straightforward and provides a clear declaration that a method should not be invoked on a mocked object.
Example Usage
Consider a scenario where we have a class UserProcessor that processes user data, and it should not delete user data under certain conditions. Let's demonstrate how to ensure that the deleteUser method of UserDataRepository is never called.
In this test, we verify that deleteUser was not called on the mockRepository mock object. This is imperative to ensure our UserProcessor behaves correctly under the specified conditions (e.g., when the second argument to processUser is false).
Summary Table
Here is a quick reference summarizing methods for checking that methods were not called:
| Method | Use Case |
verifyNoInteractions(mock) | Ensures no methods at all were called on the provided mock(s). Useful when a mock object should not be utilized during the test scenario. |
verifyNoMoreInteractions(mock) | Verifies that apart from already verified interactions, no more methods were invoked. Ideal for confirming no unexpected methods calls occur after a point. |
verify(mock, times(0)) | Directly checks that the specified method was not called a single time. This is the most direct way to assert no interaction for a specific method. |
Best Practices and Considerations
- Design for Testability: Structure your code in a way that makes it easier to isolate and test components. Dependency injection typically helps in achieving good isolatability.
- Proper Mocking: Overusing mocks can lead to fragile tests. Always consider what should be mocked and what should be real in your tests.
- Keep Tests Focused: Each test should have a clear purpose; verify interactions relevant to the behavior being tested without asserting on too many unnecessary interactions.
Testing for non-interactions effectively ensures that your code respects boundaries and adheres to specified behaviors, which are critical in maintaining robust, predictable software systems.

