Mockito provides two methods that can be used to manage the state of mocks during testing: `reset()` and `clearInvocations()`. While both methods are used to manage mock state, they serve different purposes and have distinct effects on the mocks.
Mockito's `reset()` Method
The `reset()` method in Mockito is used to reset a mock object entirely. This means it not only clears the invocation counts but also removes any stubbing that has been set on the mock. This can be problematic because it requires you to re-stub the mock after resetting it if you want to maintain specific behavior for your tests. The use of `reset()` is generally discouraged because it can lead to complex and less focused tests. It's advised to avoid using `reset()` unless absolutely necessary, as it can obscure the intent of your tests and make them harder to maintain[1][5][7].
Mockito's `clearInvocations()` Method
The `clearInvocations()` method, introduced in Mockito 2.1, allows you to clear the invocation counts of a mock without resetting its stubbing. This means you can continue to use the same stubs while ensuring that each test starts with a clean slate regarding method invocations. This method is useful when you need to verify interactions in a stateful scenario or when using dependency injection frameworks, but it should still be used sparingly. It's generally recommended to avoid using `clearInvocations()` unless necessary, as it can also complicate tests if not used thoughtfully[1][4][9].
Key Differences
- Stubbing: `reset()` removes all stubbing, requiring you to re-stub the mock, whereas `clearInvocations()` leaves stubbing intact.
- Invocation Counts: Both methods clear invocation counts, but `reset()` does so as part of a full reset, while `clearInvocations()` specifically targets invocation counts.
- Test Complexity: Both methods can lead to more complex tests if not used carefully, but `reset()` tends to cause more issues due to the need to re-stub mocks.
In summary, while both methods can be used to manage mock state, `reset()` is more invasive and generally discouraged, whereas `clearInvocations()` is more targeted but should still be used judiciously to maintain clean and focused tests.
Citations:[1] https://softwareengineering.stackexchange.com/questions/188299/is-this-an-appropriate-use-of-mockitos-reset-method
[2] https://www.lambdatest.com/automation-testing-advisor/selenium/methods/org.mockito.internal.MockitoCore.clearInvocations
[3] https://www.lambdatest.com/automation-testing-advisor/selenium/methods/org.mockito.internal.MockitoCore.clearAllCaches
[4] https://www.lambdatest.com/automation-testing-advisor/selenium/methods/org.mockito.Mockito.clearInvocations
[5] https://stackoverflow.com/questions/19441538/what-is-the-correct-alternative-to-resetting-a-mock-after-setup-in-mockito
[6] https://reflectoring.io/clean-unit-tests-with-mockito/
[7] https://stackoverflow.com/questions/30081161/mockito-does-verify-method-reboot-number-of-times
[8] https://javadoc.io/doc/org.mockito/mockito-core/2.25.1/org/mockito/Mockito.html
[9] https://wesome.org/mockito-3-clear-invocations
[10] https://github.com/mockk/mockk/discussions/842