BackendBench
Coffee

Unravel › Java concepts › Testing

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

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.

What happens, in order
  1. 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. 2@InjectMocks Service service; builds the class under test and injects the mocks
  3. 3verify(repo).save(any()) asserts that a call happened (and times(2), never() refine it)An ArgumentCaptor grabs the actual argument for inspection.
Remember
Watch out
Stubbing with when on a spy that calls the real method (use doReturn)Verifying every callMocking value classesTreating mocked behaviour as proof the real dependency works
Example (highlighted lines explain it)
@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);    }}
  1. line 1 uses Mock, the core of Mockito mocks
  2. line 3 uses Mock, the core of Mockito mocks
  3. line 4 uses InjectMocks, the core of Mockito mocks
  4. line 8 uses when, the core of Mockito mocks
  5. 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 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 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:

Part of a learning path

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.