Skip to content
Ameen Abozaid
JNR-044

Mocking

  • 6 Lessons
  • 2.5 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Mockito
  • stubs
  • mocks
  • spies
  • interaction verification

Learn first

2.5 h of practice (estimate)

Learning outcomes

  1. Distinguish stubs, mocks, spies, and fakes, and pick one.
  2. Stub return values and exceptions, and verify interactions with Mockito.
  3. Capture arguments and assert on them.
  4. Recognize over-mocking and prefer fakes or real objects where cheap.

Lessons

  1. 01Test doubles taxonomy
  2. 02Stubbing with Mockito
  3. 03Verifying interactions
  4. 04Argument captors
  5. 05When not to mock
  6. 06Demo: mocking the payment gateway's three outcomes

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)

IDOutcomeTask
JNR-044-EX011Classify 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-EX022Stub PaymentGateway.charge to approve one amount and throw GatewayTimeoutException for another; test both.
JNR-044-EX032Verify that EmailSender.sendConfirmation is called once on success and never() on decline.
JNR-044-EX043Use an ArgumentCaptor to assert the saved Order has status PAID and the correct total.
JNR-044-EX054Given a test with eight mocks and 12 verify lines, remove the unnecessary verifies and replace the repository mock with a fake.
JNR-044-EX064A test mocks HttpClient directly. Redesign it with your own RateProvider interface.
JNR-044-EX074Explain 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) and when(gateway.charge(any(), eq(b))).thenThrow(new GatewayTimeoutException()); assert the result state of each order.
  • EX03 verify(emailSender).sendConfirmation(id) and verify(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 an HttpClient adapter; mock or fake RateProvider in 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.

  1. 1A working in-memory implementation of a repository is a:O1
  2. 2A stub mainly:O1
  3. 3Which stubs a return value?O2
  4. 4To simulate a timeout you use:O2
  5. 5verify(mock, never()).refund(any()) checks that:O2
  6. 6ArgumentCaptor is used to:O3
  7. 7After verify(repo).save(captor.capture()) you read it with:O3
  8. 8Which type should you NOT mock directly?O4
  9. 9Verifying every internal call makes tests:O4
  10. 10A mock returning another mock usually signals:O4

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).

Type to search levels, phases, modules and lessons.