TOPIC 28/4016 MINNOT STARTED

Mockito and mocking in Java unit tests

A unit test should not need a real database or a real web service. Learn how Mockito stands in for them, and how to check a call was made.

L1–L4 are part of the Pro pass

L0 Explain simply

Analogy: a practice partner. A tennis player practises with a ball machine instead of a real opponent. The machine sends the ball you set up, and counts how many you hit.

It is quick, always ready and safe.

A mock is a ball machine for code. Tom, the tester at Daily List, a to-do list app, uses it so a small test needs no real database.

The picture is only a first way in. It will not fit every detail, so the next levels give the exact words and show where the picture stops working.

PICTURE IT

The picture is the one the lesson above opens with. The table below shows how each part of it matches the software.

THE REAL JOB Practising with a ball machine instead of an opponent.

  1. Mock
  2. Call
  3. Check
Ball machine

HOW THE PICTURE MAPS

How each part of the picture matches the software
In the pictureIn the software
The machineA mock
The ball you setwhen and thenReturn
Counting your hitsverify
A real opponentThe real part

WHERE THIS PICTURE STOPS

  • A machine is built, but a mock is made in code.
  • A machine is safe, but a mock can hide a real fault.
  • Mocks record calls and can return stubbed values or defaults; neither proves the real integration works.

HOOK

Set, call, check.

TELL A FRIEND

A ball machine: set the ball, hit it, count.

OFTEN MIXED UP WITH Test runners: JUnit, TestNG and pytest. That topic explains the runner that finds and runs tests. This one adds the mocks a unit test uses.

PRACTISE AND KEEP

Sources for this topic — 2 sources
  1. Market practice

    Mockito tutorial (vogella) ↗ — vogella · a Mockito tutorial site: a mock is created with Mockito, a reply is set with when and thenReturn, calls are checked with verify, and spies and argument matchers are also covered (search excerpt, page not opened)

    Describes creating mocks, setting a reply with when and thenReturn, checking calls with verify, and spies and argument matchers. Tutorial site. Practitioner guidance, not a standard. · Checked 2026-10-31

    Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.

  2. Market practice

    JUnit 5 user guide (JUnit documentation) ↗ — JUnit team · JUnit 5 user guide: tests are methods marked with annotations and run by the JUnit platform (search excerpt, page not opened)

    Official guide. Per the search excerpt, JUnit Jupiter is the model for writing tests in JUnit 5, the @Test annotation marks a test method, and the Assertions class holds checks. The result list showed many guide versions; the 5.14.0 page was the newest seen. Tool documentation, not a standard. · Checked 2026-10-18

    Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page for the 5.14.0 guide as listed in a search; read as a search excerpt only. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.

Deepest material on this page: L4 — Standards & sources. Where a topic stops short of implementation depth, that is a deliberate coverage decision, not an oversight — see coverage.