Skip to content

Mock

A mock is a test double pre-programmed with expectations about the calls it should receive; unlike a stub, it fails the test when those expectations are not met, enabling behaviour verification.

Definition

What it is

A mock is a test double with a contract. You tell it, before the test runs, which methods will be called, with which arguments, and how many times. When the test ends, the mock checks the ledger. If the ledger does not match the contract, the test fails.

That is the whole difference from a stub. A stub only answers. A mock answers and judges.

Why it matters

Some bugs live in the conversation, not in the result. An email service that sends the right message to the wrong recipient may produce a correct final state in the test database, while having called the wrong method. A mock catches that. A stub does not.

Mock vs stub: the 2007 line

Aspect

Stub

Mock

What it checks

The state left after the call

The calls made during the test

Verification style

State verification

Behaviour verification

Fails on unmet expectation

No

Yes

Coupling to implementation

Low

High

Best for

Isolating a dependency

Asserting a collaboration

Mocks are objects pre-programmed with expectations which form a specification of the calls they are expected to receive.

— Martin Fowler, *Mocks Aren't Stubs*, 2 January 2007

The five doubles

graph TD
  TD[Test Double] --> D[Dummy: passed but never used]
  TD --> S[Stub: canned answers]
  TD --> SP[Spy: records calls, you assert later]
  TD --> M[Mock: asserts expectations itself]
  TD --> F[Fake: working shortcut]

How to use one without regretting it

A mock that survives refactoring

  1. Mock the boundary

    Mock the external collaborator (the payment gateway), not the internal helper.

  2. Expect the minimum

    One call with one argument is a contract. Twelve calls with ordered arguments is a straitjacket.

  3. Prefer state when possible

    If the final state is enough, use a stub and assert the state.

  4. Never mock third-party SDKs directly

    Wrap them in an adapter and mock the adapter. The SDK will change; your test should not.

Common failure: the mock that knows too much

Origin

The word entered English in the 15th century from Old French mocquer, "to laugh at, to deride". The sense of "imitation, counterfeit" appears by the 18th century ("a mock trial", "mock turtle soup"). In the 1990s, the object-oriented testing community borrowed it for objects that imitate a collaborator and check that the imitation was used correctly.

Evidence

FAQ

Mock or stub?

Use a stub when you only need a dependency to answer. Use a mock when the test's purpose is to assert that a specific collaboration happened.

What is behaviour verification?

Checking the calls made during the test, instead of the state left after it.

Why are mocks controversial?

Because they couple the test to the implementation. A refactor that preserves behaviour can break every mock in the suite.

Can a mock replace an integration test?

No. A mock proves the unit asked for the right thing; only an integration test proves the real system does the right thing.

Connections

Etymology

Origin: borrowed

  • mock (verb): from Old French mocquer, to deride; by the 18th century, to imitate

Semantic network

MockTest doublsoftware-tStubFixtureSpyFake

● broader ● narrower ● related

Broader

Related

Used in the garden

Sources