Mockito Deep Dive — @Mock, @Spy, verify(), ArgumentCaptor
Fast recall
Quick revision
Core answer
Whether the candidate understands Mockito's core APIs beyond basic mocking and can use verify(), ArgumentCaptor, and stubbing strategies effectively.
Using when().thenReturn() with spies instead of doReturn().when() — the former calls the real method before stubbing, causing side effects.
Explain ArgumentCaptor for asserting what was passed to a dependency, verify() with times/never for interaction testing, and BDD-style given/when/then aliases.
Interview-ready guide
Interview answer
@Mock creates stubs that return defaults. @Spy wraps real objects — real methods execute unless overridden with doReturn().when(). The key mistake is using when().thenReturn() with spies, which calls the real method before stubbing.
verify() confirms interactions — did the service call save()? Did it send the notification? verify(mock, never()) is equally important for testing that error paths don't trigger side effects.
ArgumentCaptor captures arguments passed to mocks for detailed assertions. When verify() tells you the method was called, the captor tells you what was passed. This is essential for testing services that transform data between layers — asserting that the request DTO was correctly mapped to the domain object.
My pattern: stub the inputs, call the method, verify the interactions, capture arguments for important assertions.
Mockito Deep Dive — @Mock, @Spy, verify(), ArgumentCaptor
Direct Answer
Mockito's @Mock creates fakes that return defaults. @Spy wraps real objects. verify() confirms interactions happened. ArgumentCaptor captures arguments passed to mocks for detailed assertions.
Common Mistakes & Pitfalls
Using when().thenReturn() with spies instead of doReturn().when() — the former calls the real method before stubbing, causing side effects.
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.