Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How does Mockito's reset method affect stubbing


How does Mockito's reset method affect stubbing


Mockito's `reset` method is used to reset a mock object, which means it clears all stubbing and interactions recorded on the mock. This can be useful in certain scenarios where you want to reuse a mock object with different stubbing configurations across multiple tests or within a single test method. However, using `reset` is generally discouraged because it can indicate overly complex tests that are trying to test multiple scenarios at once, which goes against the principle of testing one thing per test.

How `reset` Affects Stubbing

When you call `reset` on a mock object, it forgets any stubbing you have set up for it. This means that any `thenReturn`, `thenThrow`, or other stubbing configurations are removed. After resetting, the mock behaves as if it were freshly created, with no predefined behavior for its methods.

Here is an example of how `reset` affects stubbing:

java
List mockList = mock(List.class);
when(mockList.size()).thenReturn(10); // Stubbing size() to return 10

// At this point, calling mockList.size() will return 10

reset(mockList); // Reset the mock

// After reset, calling mockList.size() will return the default value (0 for size())

In this example, after calling `reset`, the stubbing for `size()` is removed, and the mock will behave according to its default behavior, which typically returns `0` for `size()`.

Use Cases for `reset`

While `reset` is not commonly used, there are scenarios where it might be necessary:

1. Reusing Mocks Across Tests: If you have a setup method that creates mocks and you want to reuse these mocks across multiple tests with different stubbing configurations, you might use `reset` to clear the stubbing between tests.

2. Complex Test Scenarios: In some cases, you might need to test different behaviors within a single test method. Using `reset` can help reset the mock to a clean state before applying new stubbing configurations.

However, it's generally better to create separate tests for each scenario rather than using `reset` to change the behavior of a mock within a single test.

Best Practices

- Avoid Overly Complex Tests: Try to keep each test focused on a single scenario. If you find yourself needing to use `reset` frequently, it might be a sign that your tests are too complex.

- Use Separate Tests: Instead of using `reset` to change mock behavior within a test, create separate tests for each scenario.

- Use Consecutive Stubbing: If you need to simulate different behaviors for consecutive calls to the same method, consider using Mockito's consecutive stubbing features instead of `reset`. For example, you can use `thenThrow` followed by `thenReturn` to simulate an exception on the first call and a return value on subsequent calls.

java
when(mockList.size()).thenThrow(new RuntimeException()).thenReturn(10);

This approach allows you to define different behaviors for consecutive method calls without needing to reset the mock.

Citations:
[1] https://site.mockito.org/javadoc/current/org/mockito/Mockito.html
[2] https://www.toptal.com/java/a-guide-to-everyday-mockito
[3] https://stackoverflow.com/questions/4180209/mockito-re-stub-method-already-stubbed-with-thenthrow
[4] https://github.com/mockito/mockito/issues/773
[5] https://stackoverflow.com/questions/18164123/how-to-clean-up-mocks-in-spring-tests-when-using-mockito
[6] https://www.vogella.com/tutorials/Mockito/article.html
[7] https://stackoverflow.com/questions/4180209/mockito-re-stub-method-already-stubbed-with-thenthrow/43917060
[8] https://softwareengineering.stackexchange.com/questions/254852/using-mockito-reset
[9] https://www.tutorialspoint.com/mockito/mockito_resetting_mock.htm
[10] https://softwareengineering.stackexchange.com/questions/188299/is-this-an-appropriate-use-of-mockitos-reset-method