Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Are there any limitations to using never() with Mockito


Are there any limitations to using never() with Mockito


Using `never()` with Mockito is a powerful tool for verifying that certain methods on mock objects are not called during a test. However, there are some limitations and considerations when using this method:

1. Static Methods**

- Limitation: Mockito does not support mocking static methods directly. If you need to verify that a static method is not called, you would typically use a library like PowerMock, which provides additional functionality for mocking static methods. However, PowerMock is generally discouraged due to its complexity and potential for overuse.
- Workaround: Refactor your code to avoid static methods if possible, or use PowerMock with caution.

2. Argument Matchers**

- Limitation: When using `never()` with methods that have parameters, you must specify argument matchers for each parameter if you want to verify that the method is never called with any combination of parameters.
- Workaround: Use Mockito's argument matchers like `any()`, `anyInt()`, `anyString()`, etc., to match any possible input. For example:
java
     verify(mockObject, never()).someMethod(anyInt(), anyString());
     

3. Test Brittleness**

- Limitation: Overusing `verifyNoMoreInteractions()` or `verifyZeroInteractions()` can make tests brittle, as they fail if any additional interactions occur, even if those interactions are irrelevant to the test's purpose.
- Workaround: Prefer using `never()` for specific methods instead of verifying no interactions at all. This approach makes tests more focused and less prone to breakage due to unrelated changes.

4. POJOs and Value Objects**

- Guideline: Mockito advises against mocking value objects (POJOs) unless absolutely necessary. Instead, create instances of these objects or use builders/factory methods to simplify their creation in tests.
- Workaround: If a POJO is complex to instantiate, consider creating a builder or factory method specifically for testing purposes.

5. Spying**

- Limitation: When using spies (partial mocks), `never()` behaves as expected, but ensure that the underlying method is not called unintentionally when setting up the spy.
- Workaround: Use `doReturn()` or `doThrow()` instead of `when()` to avoid calling the real method when setting up a spy.

In summary, while `never()` is a valuable tool for ensuring that certain interactions do not occur, its use should be balanced with good testing practices to avoid over-specification and brittleness in tests.

Citations:
[1] https://www.javaguides.net/2023/10/mockito-never.html
[2] https://stackoverflow.com/questions/19302207/how-to-verify-mocked-method-not-called-with-any-combination-of-parameters-using
[3] https://softwareengineering.stackexchange.com/questions/363112/is-it-really-a-bad-practice-to-mock-a-pojo-value-object-if-you-dont-care-abou
[4] https://www.vogella.com/tutorials/Mockito/article.html
[5] https://groups.google.com/g/powermock/c/mAGhd72iTE0
[6] https://reflectoring.io/clean-unit-tests-with-mockito/
[7] https://discuss.kotlinlang.org/t/how-to-best-use-mockito-in-kotlin/24675
[8] https://www.dhiwise.com/post/mastering-unit-testing-a-complete-guide-to-mockito-verify
[9] https://site.mockito.org/javadoc/current/org/mockito/Mockito.html