Mocking
On this page 8 topics
Overview
Topics
- Mockito
- stubs
- mocks
- spies
- interaction verification
Learn first
2.5 h of practice (estimate)
Learning outcomes
- Distinguish stubs, mocks, spies, and fakes, and pick one.
- Stub return values and exceptions, and verify interactions with Mockito.
- Capture arguments and assert on them.
- Recognize over-mocking and prefer fakes or real objects where cheap.
Lessons
Lab and exercises
Lab
JNR-044-LAB1 · Order service tests
Outcomes: 1–4 · Time: ~2.5 h Test OrderService with a mocked payment gateway (success, decline, timeout) and an in-memory fake repository. Acceptance: tests survive an internal refactor of the service.
Exercises (assessment: refactor an over-mocked test suite)
| ID | Outcome | Task |
|---|---|---|
| JNR-044-EX01 | 1 | Classify these as stub, mock, spy, or fake: canned exchange rate, in-memory ProductRepository, email sender checked for a call, real calculator with call counting. |
| JNR-044-EX02 | 2 | Stub PaymentGateway.charge to approve one amount and throw GatewayTimeoutException for another; test both. |
| JNR-044-EX03 | 2 | Verify that EmailSender.sendConfirmation is called once on success and never() on decline. |
| JNR-044-EX04 | 3 | Use an ArgumentCaptor to assert the saved Order has status PAID and the correct total. |
| JNR-044-EX05 | 4 | Given a test with eight mocks and 12 verify lines, remove the unnecessary verifies and replace the repository mock with a fake. |
| JNR-044-EX06 | 4 | A test mocks HttpClient directly. Redesign it with your own RateProvider interface. |
| JNR-044-EX07 | 4 | Explain what "mocks returning mocks" says about the design and how to fix it. |
Show solutions
- EX01 Stub, fake, mock, spy.
- EX02
when(gateway.charge(any(), eq(a))).thenReturn(approved)andwhen(gateway.charge(any(), eq(b))).thenThrow(new GatewayTimeoutException()); assert the result state of each order. - EX03
verify(emailSender).sendConfirmation(id)andverify(emailSender, never()).sendConfirmation(any()). - EX04
verify(repo).save(captor.capture()); assertThat(captor.getValue().status()).isEqualTo(PAID);plus a total assertion. - EX05 Keep only side-effect verifies (charge, email); assert on returned order and fake repository content.
- EX06 Define
interface RateProvider { BigDecimal rate(String code); }implemented by anHttpClientadapter; mock or fakeRateProviderin unit tests; test the adapter separately. - EX07 The code reaches through several objects (law of Demeter) or the test is too coupled; pass the needed value in, or return a simple value from the first collaborator.
- Outcome 1 is also covered by quiz Q01–Q02; the "survives a refactor" acceptance in the lab covers outcome 4.
Quiz
Outcomes: O1 test doubles · O2 stubbing and verifying · O3 captors · O4 over-mocking.
A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.
Practical part
Seven exercises JNR-044-EX01 to EX07 plus the lab; for the assessment, refactor a provided over-mocked suite so it still passes after an internal refactor. Pass: quiz 8/10, at least five exercises correct, and the refactor accepted.
Common mistakes
Mocking types you do not own; verifying every call; mocks returning mocks.
Assessment
Refactor an over-mocked test suite.
Career relevance
Contract testing (MID-045).