Mockito's `reset` method can be used with partial mocks, but it resets the entire mock, including any stubbing or interactions recorded on it. This means that if you have a partial mock (created using `spy`) and you call `reset` on it, all stubbing and recorded interactions will be cleared, but the original behavior of the object will still be present for methods that were not stubbed.
Using `reset` with Partial Mocks
When you use `reset` on a partial mock, it will remove any stubbing you have set up, so you will need to re-stub methods if you want them to behave differently than their original implementation. However, the original behavior of the methods that were not stubbed will remain unchanged.
Here's an example of how you might use `reset` with a partial mock:
java
// Create a partial mock
Stock stock = spy(new Stock(10.0, 20));
// Stub some methods
when(stock.getPrice()).thenReturn(100.0);
when(stock.getQuantity()).thenReturn(200);
// Use the partial mock
double value = stock.getValue(); // This will use the stubbed getPrice and getQuantity
// Reset the mock
reset(stock);
// After reset, stubbing is lost, but original behavior remains
// You need to re-stub methods if you want custom behavior again
when(stock.getPrice()).thenReturn(150.0);
when(stock.getQuantity()).thenReturn(300);
// Now, getValue will use the new stubbing
value = stock.getValue();
Considerations
While `reset` can be used with partial mocks, it is generally discouraged because it can lead to complex tests that are harder to understand and maintain. Instead of using `reset`, it's often better to write separate test methods for different scenarios, each setting up the mock or partial mock as needed from scratch[4][5].
If you need to reset interactions without losing stubbing, consider using `Mockito.clearInvocations` instead of `reset`, as it only clears the recorded interactions without affecting stubbing[11].
Partial Mocking with Mockito
To create a partial mock, you use the `spy` method instead of `mock`. This allows you to mock specific methods while keeping the original behavior of others:
java
Stock stock = spy(new Stock(10.0, 20));
when(stock.getPrice()).thenReturn(100.0);
when(stock.getQuantity()).thenReturn(200);
In this example, `getValue` will still perform the multiplication using the stubbed `getPrice` and `getQuantity` methods[3][9].
Conclusion
While Mockito's `reset` method can be used with partial mocks, it should be used sparingly and with caution. It's generally better to structure your tests to avoid needing to reset mocks mid-test. If you do need to reset interactions, consider using `clearInvocations` to avoid losing stubbing.
Citations:[1] https://www.tutorialspoint.com/mockito/mockito_resetting_mock.htm
[2] https://www.lambdatest.com/automation-testing-advisor/selenium/methods/org.mockito.StateMaster.reset
[3] https://community.lambdatest.com/t/can-mockito-create-a-partial-mock-with-some-real-method-behavior/31528
[4] https://site.mockito.org/javadoc/current/org/mockito/Mockito.html
[5] https://stackoverflow.com/questions/34412106/appropriate-use-of-mockito-reset
[6] https://mockk.io
[7] https://stackoverflow.com/questions/28235171/how-to-reset-mock-spy-partially
[8] https://groups.google.com/g/mockito/c/5G-XnpIhP3s
[9] https://stackoverflow.com/questions/14970516/use-mockito-to-mock-some-methods-but-not-others
[10] https://www.lambdatest.com/automation-testing-advisor/selenium/methods/org.mockito.Mockito.reset
[11] https://softwareengineering.stackexchange.com/questions/188299/is-this-an-appropriate-use-of-mockitos-reset-method