Unravel › Java concepts › Testing
Mockito mocks
Mockito creates fake versions of a class's collaborators so a test can control what they return and check how they were used, without a real database or network.
Learn these first
At a glance
A flight simulator: instead of a real plane, the pilot trains against a stand-in that behaves exactly as scripted and records every control touched.
- 1@Mock Repo repo; creates a fakewhen(repo.find(1L)).thenReturn(user) scripts an answer; unscripted calls return null, 0, false or an empty collection.
- 2@InjectMocks Service service; builds the class under test and injects the mocks
- 3verify(repo).save(any()) asserts that a call happened (and times(2), never() refine it)An ArgumentCaptor grabs the actual argument for inspection.
- Mock what you own the boundary of (repositories, HTTP clients), not simple value objects
- Mockito generates a subclass (or uses an inline agent) at runtime, intercepts method calls, and records and matches them against stubbed answers
@ExtendWith(MockitoExtension.class)class OrderServiceTest { @Mock OrderRepository repository; @InjectMocks OrderService service; @Test void loadsOrder() { when(repository.findById(1L)).thenReturn(Optional.of(new Order(1L))); assertEquals(1L, service.find(1L).getId()); verify(repository).findById(1L); }}- line 1 uses Mock, the core of Mockito mocks
- line 3 uses Mock, the core of Mockito mocks
- line 4 uses InjectMocks, the core of Mockito mocks
- line 8 uses when, the core of Mockito mocks
- line 10 uses verify, the core of Mockito mocks
Read the full explanation (about 1 min)
Mockito creates fake versions of a class's collaborators so a test can control what they return and check how they were used, without a real database or network.
A mental model
A flight simulator: instead of a real plane, the pilot trains against a stand-in that behaves exactly as scripted and records every control touched.
How it works
@Mock Repo repo; creates a fake. when(repo.find(1L)).thenReturn(user) scripts an answer; unscripted calls return null, 0, false or an empty collection.
@InjectMocks Service service; builds the class under test and injects the mocks.
verify(repo).save(any()) asserts that a call happened (and times(2), never() refine it). An ArgumentCaptor grabs the actual argument for inspection.
Going deeper
Mock what you own the boundary of (repositories, HTTP clients), not simple value objects. Over-verifying interactions couples tests to implementation, so they break at every refactor. If a test needs a dozen mocks the class probably has too many dependencies. A mock cannot find mistakes in the real database query; integration tests are still needed.
Under the hood
Mockito generates a subclass (or uses an inline agent) at runtime, intercepts method calls, and records and matches them against stubbed answers.
Common mistakes
- Stubbing with
whenon a spy that calls the real method (usedoReturn). - Verifying every call.
- Mocking value classes.
- Treating mocked behaviour as proof the real dependency works.
How Unravel recognises it
When you paste code, Unravel looks for these signs of mockito mocks. Spotting them yourself is a good habit when you read unfamiliar code:
- Annotations:
@Mock@InjectMocks@Spy@MockBean@Captor@MockitoSettings - Types:
MockitoArgumentCaptor - Method calls:
when()verify()thenReturn()thenThrow()mock()
Part of a learning path
- Reliable code · previous: JUnit tests
- Testing enterprise code · previous: Advanced JUnit 5 and async assertions · next: Advanced Mockito (matchers, captors, static mocks)
Related concepts
Builds on this
Frequently asked questions
What is Mockito mocks?
Mockito creates fake versions of a class's collaborators so a test can control what they return and check how they were used, without a real database or network.
What are the most common mistakes with Mockito mocks?
Stubbing with when on a spy that calls the real method (use doReturn). Verifying every call. Mocking value classes. Treating mocked behaviour as proof the real dependency works.
How can I see Mockito mocks in real code?
Paste your own Java into Unravel and it will point out where mockito mocks appears, explain how it works at the detail level you choose, and draw the flow of the code around it.