Stubbing in Mockito means telling a mock what to return, or throw, when a specific method is called with specific arguments. You do it with when(mock.method(args)).thenReturn(value) or, for void and spy cases, doReturn(value).when(mock).method(args). Without stubbing, a mock returns defaults: null, 0, false, or an empty collection or Optional.
Stubbing isolates the class under test from its collaborators, such as a repository or HTTP client, so the test is fast and deterministic. The examples use Mockito 5 with JUnit 5.
Create a mock, stub a call, and exercise the code under test:
Mockito returns Optional.empty() for Optional return types and empty collections for collections, so the second test needs no stubbing.
Use thenThrow to simulate failures, and chain results for consecutive calls:
For a void method, the when(...) form does not compile, so use the do... family:
Matchers from ArgumentMatchers stub a family of calls:
If you use a matcher for one argument, you must use matchers for all arguments in that call (wrap raw values with eq(...)). Prefer exact values when you can, because loose matchers hide wrong arguments.
Compute the result from the arguments:
For a repository save method that returns its argument, thenAnswer(inv -> inv.getArgument(0)) or AdditionalAnswers.returnsFirstArg() is the usual idiom.
A spy wraps a real object. With when(spy.method()) the real method runs during stubbing, which can cause side effects or exceptions. Use doReturn instead:
Use spies sparingly, since they often signal that the class has too many responsibilities.
Mockito 5 uses the inline mock maker by default, so you can mock final classes and methods without extra configuration. Static methods can be stubbed in a limited scope with mockStatic:
Always use try-with-resources so the static mock is closed. Prefer injecting a java.time.Clock instead of mocking statics.
With @ExtendWith(MockitoExtension.class) Mockito runs in strict mode and fails the test with UnnecessaryStubbingException if you stub something that is never used. That keeps tests honest. Other habits to keep:
verify checks interactions.| Technique | Purpose | Example |
|---|---|---|
| Stubbing | Provide canned answers to calls | when(repo.find(1)).thenReturn(x) |
| Verifying | Check that a call happened | verify(repo).save(x) |
| Argument capture | Inspect arguments passed | ArgumentCaptor<User> |
Defaults by type: null for objects, 0 for numbers, false for booleans, and empty collections, streams and Optionals.
Use doReturn for spies, for void methods (with doNothing or doThrow), and when calling the real method during stubbing is undesirable.
You mixed matchers and raw values in one call. Use matchers for every argument, wrapping literals with eq.
No. Test private behaviour through the public API, or reconsider the design.
Stubbing defines how a mock responds. Use when/thenReturn for normal calls, thenThrow and the do... family for exceptions, void methods and spies, and matchers for flexible arguments. Keep stubs minimal and rely on strict stubs to catch unused ones.