The Complete Mockito Guide
Everything you need to know about Mockito in one place — @Mock, @InjectMocks, stubbing, verification, ArgumentCaptor, Spy, Spring Boot MockBean, and interview Q&A.
Advertisement
📋 What's in This Guide
1. Setup & Dependencies
Add Mockito to your project — Maven & Gradle
Mockito ships inside spring-boot-starter-test (no extra dependency needed for Spring Boot projects). For plain Java, add:
spring-boot-starter-test, Mockito is already on the classpath. Just annotate your test with @ExtendWith(MockitoExtension.class) and you're ready.2. Core Annotations
@Mock, @InjectMocks, @Spy, @Captor — when to use each
@Mock
Creates a fully fake object. Every method returns a zero/null/empty default unless you stub it. The class is never instantiated — no real code runs.
@InjectMocks
Creates the real class under test and injects all @Mock fields into it via constructor injection, then setter injection, then field injection.
@Spy
Wraps a real object. Unstubbed method calls execute the real implementation; stubbed calls return your defined value. Useful for partial mocking.
@Captor
Declares an ArgumentCaptor inline. Use it with verify() to capture and inspect the argument passed to a mock method call.
3. Stubbing — when / thenReturn / thenThrow
Control what a mock returns when a method is called
InvalidUseOfMatchersException.4. Stubbing Void Methods
when().thenReturn() does not work for void — use doXxx() instead
| Syntax | Use case | Works on void? |
|---|---|---|
when(m.foo()).thenReturn(x) | Stub return value | ❌ No |
when(m.foo()).thenThrow(ex) | Stub exception (non-void) | ❌ No |
doReturn(x).when(m).foo() | Stub return (avoids real call on Spy) | ❌ No |
doNothing().when(m).foo() | Suppress void method | ✅ Yes |
doThrow(ex).when(m).foo() | Throw from void method | ✅ Yes |
doAnswer(inv -> …).when(m).foo() | Custom logic | ✅ Yes |
5. Verification
Assert how many times — and with what arguments — a method was called
6. ArgumentCaptor
Capture the exact argument passed to a mock to assert its content
Use ArgumentCaptor when you need to inspect the full object passed to a mock — not just verify it was called.
eq() / any() matchers for simple assertions. Use ArgumentCaptor when you need to assert multiple fields of the captured object, or when the object is built internally by the class under test.7. @Spy — Partial Mocking
Wrap a real object and stub only what you need
| Feature | @Mock | @Spy |
|---|---|---|
| Object type | Fully fake (proxy) | Real object (wrapped) |
| Unstubbed method | Returns null/0/false | Calls real method |
| Use case | Isolate dependencies | Partial mock of a real class |
| Risk | Stubs may drift from real behaviour | Real calls can have side-effects |
when(spy.method()).thenReturn(x) on a Spy — the real method is called during the stubbing. Always use doReturn(x).when(spy).method() instead.8. Spring Boot Integration — @MockBean & MockMvc
Replace Spring beans with Mockito mocks in slice and full context tests
| Annotation | Context | When to use |
|---|---|---|
@Mock | No Spring context | Pure unit tests — fast |
@MockBean | Spring context (loaded) | Controller slice tests (@WebMvcTest), integration tests (@SpringBootTest) |
@SpyBean | Spring context (loaded) | Spy on a real Spring bean |
reset() manually. Use @SpyBean when you need both the real Spring bean behaviour and the ability to verify interactions on it.9. Common Mistakes & Anti-Patterns
❌ Mocking the class under test
Never put @Mock on the class you are actually testing. That class should be annotated with @InjectMocks (or constructed manually) and receive real execution.
❌ Over-verifying
Avoid verifying every single interaction. Verify only the interactions that are meaningful to the test's intent. verifyNoMoreInteractions() on every test makes refactoring painful.
❌ Mocking value objects
Don't mock simple data holders (Order, User). Create real instances. Mocking them adds noise and hides the actual structure being tested.
❌ Stubbing on Spy with when()
On a @Spy, when(spy.method()).thenReturn(x) calls the real method during stubbing. Always use doReturn(x).when(spy).method() to avoid side-effects.
❌ Mixing matchers and literals
when(m.foo("exact", any())) will throw InvalidUseOfMatchersException. Either use exact values for all args, or wrap all args with matchers like eq("exact").
❌ Static methods (classic Mockito)
Mockito 5 supports mocking statics with mockStatic() (try-with-resources). But prefer refactoring static calls behind an interface instead — it makes code more testable long-term.
10. Mockito Interview Questions & Answers
The questions you'll actually be asked — with crisp, complete answers
Q1. What is Mockito and why is it used?
Mockito is a Java mocking framework that creates fake (mock) objects for unit testing. It lets you isolate the class under test from its dependencies (database, HTTP client, etc.), define what each mock should return when called, and verify that specific methods were called. It integrates with JUnit 5 via @ExtendWith(MockitoExtension.class).
Q2. What is the difference between @Mock and @InjectMocks?
@Mock creates a mock (fake) instance of a class or interface — no real code runs. @InjectMocks creates the real instance of the class under test and automatically injects all @Mock and @Spy fields into it (constructor → setter → field injection).
Q3. @Mock vs @Spy — when do you use each?
Use @Mock when you want a completely fake object — no real logic runs at all. Use @Spy when you want the real object's behaviour for most methods, but need to stub or verify one or two specific methods without executing their real code.
Q4. How do you stub a void method in Mockito?
You cannot use when(...).thenReturn() for void methods. Instead use: doNothing().when(mock).voidMethod(), doThrow(ex).when(mock).voidMethod(), or doAnswer(inv -> null).when(mock).voidMethod().
Q5. What is ArgumentCaptor and when should you use it?
ArgumentCaptor captures the actual argument passed to a mocked method so you can assert its value. Use it when the argument is constructed inside the class under test and you cannot pass it in directly. Declare with @Captor or ArgumentCaptor.forClass(), call verify(mock).method(captor.capture()), then inspect with captor.getValue().
Q6. What is the difference between @Mock and @MockBean?
@Mock (plain Mockito) creates a mock with no Spring context — very fast. @MockBean (spring-boot-test) creates a Mockito mock AND registers it as a bean inside the loaded Spring ApplicationContext, replacing any existing bean of that type. Use @MockBean in @WebMvcTest or @SpringBootTest; use @Mock in pure unit tests.
Q7. Can you mock static methods in Mockito 5?
Yes. Use MockedStatic<T> mocked = mockStatic(MyClass.class) inside a try-with-resources block. Inside the block, mocked.when(() -> MyClass.staticMethod()).thenReturn(value) stubs the static call. Best practice is to avoid static coupling by injecting the static behaviour through an interface, which makes the design more testable without needing mockStatic.
Q8. What does verifyNoMoreInteractions() do and when should you avoid it?
It asserts that no other methods were called on the mock beyond those already verified. Avoid adding it to every test — it couples your tests tightly to implementation details, making refactoring expensive even when behaviour hasn't changed. Use it only when unexpected interactions represent a real bug risk.
Q9. What happens if @InjectMocks cannot inject — does it fail silently?
Yes — Mockito silently skips injection if no matching constructor, setter, or field is found, rather than throwing an exception. This is a known pitfall. Always write a smoke-test assertion (e.g., assertNotNull(service)) or use constructor injection to make the dependency explicit and avoid silent failures.
Q10. How do you test a method that calls another method on the same class?
If method A calls method B on the same object, a plain @Mock cannot help (it mocks the entire class). Use a @Spy on the real class, then stub only method B: doReturn(value).when(spy).methodB(). Alternatively, extract the inner logic into a collaborator and inject it — which is cleaner and avoids partial mocking.
📚 Mockito Learning Path
Follow this order to go from zero to production-ready test suites
Step 1 — First Tests
Step 2 — Intermediate
All Mockito Articles on This Site
Every Mockito tutorial — with topic and difficulty level
| Article | Topic Focus | Level | Date |
|---|---|---|---|
| Mockito Tutorial for Beginners (2026) — Step-by-Step Guide | @Mock, when/thenReturn, verify | Beginner | 2026-04-10 |
| Mockito Tutorial with Real-Time Examples (Beginner to Advanced) | Full lifecycle — mock → stub → verify | Intermediate | 2026-04-10 |
| Mockito Tutorial — Unit Testing Made Easy | Setup, annotations, common patterns | Beginner | 2026-04-09 |
| Mockito Tutorial for Beginners Step by Step with Examples | @InjectMocks, thenReturn, thenThrow | Beginner | 2026-04-09 |
| Mockito with JUnit — Complete Testing Guide with Examples | JUnit 5 + Mockito integration | Intermediate | 2026-04-09 |
| Spring Boot Unit Testing with JUnit 5 and Mockito | @MockBean, @WebMvcTest, MockMvc | Intermediate | 2026-04-06 |
| Spring Boot Testing Interview Questions — JUnit & Mockito 2026 | Interview prep — @MockBean, verify, ArgumentCaptor | Advanced | 2026-03-25 |
| Mastering Spring Boot Unit Testing with JUnit 5 and Mockito | Deep dive — Spy, Captor, SliceTests | Advanced | 2026-03-24 |
| Getting Started With Mockito and JUnit | First Mockito test — absolute basics | Beginner | 2023-01-08 |
Read Next
Master Mockito Once — Write Better Tests Forever
Bookmark this page as your go-to Mockito reference. 9 deep-dive articles, all linked above.
Search All Mockito Posts →