Resetting Mocks Between Tests
Fast recall
Quick revision
Core answer
Mockito.reset(mock) removes both stubbings and recorded interactions, returning the mock close to a newly created state. clearInvocations(mock) removes only recorded calls and keeps stubbings. Most tests should get fresh mocks for each test method instead of resetting shared state. Reset can be reasonable when a container supplies the same injected mock to several phases that cannot be rebuilt cheaply, but it should be local and intentional.Interview-ready guide
Interview answer
Mockito.reset(mock) removes every stub and recorded interaction from the mock. The Java reference stays the same, but the mock returns to its default behavior. clearInvocations(mock) is narrower: it forgets recorded calls while preserving the stubs.
For ordinary unit tests I do not use either as cleanup. With JUnit's default per-method lifecycle and MockitoExtension, each test receives newly initialized instance mocks, which keeps tests independent. Shared static mocks or a PER_CLASS lifecycle need separate care.
Reset can be useful when a Spring context or another expensive container has injected one mock and a test deliberately needs a new phase without rebuilding that container. If only setup interactions should be ignored, clearInvocations is less destructive. In either case I keep the operation local and explain why a fresh object is not practical.
Resetting Mocks Between Tests
Direct Answer
Mockito.reset(mock) removes both stubbings and recorded interactions, returning the mock close to a newly created state. clearInvocations(mock) removes only recorded calls and keeps stubbings. Most tests should get fresh mocks for each test method instead of resetting shared state. Reset can be reasonable when a container supplies the same injected mock to several phases that cannot be rebuilt cheaply, but it should be local and intentional.
Code Example
// ANTI-PATTERN: Shared mock with manual reset
class BadOrderServiceTest {
private static PaymentGateway sharedGateway = mock(PaymentGateway.class); // shared!
@AfterEach
void cleanup() {
Mockito.reset(sharedGateway); // trying to clean up between tests
}
@Test
void testOne() { /* uses sharedGateway */ }
@Test
void testTwo() { /* reuses same mock, tests coupled */ }
}
// CORRECT: Per-test mock creation
@ExtendWith(MockitoExtension.class)
class GoodOrderServiceTest {
@Mock // Fresh mock for each test automatically
private PaymentGateway paymentGateway;
@InjectMocks
private OrderService orderService;
@Test
void testOne() { /* uses fresh mock */ }
@Test
void testTwo() { /* different fresh mock, fully isolated */ }
}
Common Mistakes & Pitfalls
- Shared mutable mocks: class-level or static mocks can make tests order-dependent.
- Resetting in every cleanup method: a fresh per-test mock is usually simpler.
- Confusing the two operations:
resetremoves stubs and calls;clearInvocationsremoves calls only. - Resetting too early: later verification cannot see erased interactions.
- Using reset to hide a long scenario: separate tests often express phases more clearly.
Check your understanding
Answer these without looking back at the article.
- How does @MockBean interact with Spring's application context caching?
- What is clearInvocations() and how is it different from reset()?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.