liveBook · Manning

Test double

As explained in Unit Testing Principles, Practices, and Patterns

A test double is an object that resembles a production dependency but is simplified to reduce complexity during testing.

A test double is an object that looks and behaves like a production dependency but is simplified to reduce complexity during testing. Replacing dependencies can isolate a system under test, narrow the source of failures, and split a complicated object graph into manageable pieces. section 2.1

Test doubles cover several roles, grouped into two behavioral types:

  • Mocks and spies emulate and examine outgoing interactions: calls that change a dependency’s state. section 5.5
  • Stubs supply incoming interaction data without representing the final result. section 5.5
  • Dummies and fakes serve the same broad role as stubs; a mocking library can create either mocks or stubs. section 5.5

The isolation strategy is a trade-off. The London style replaces mutable collaborators, usually treating a class as the unit under test. The classical style replaces dependencies shared between tests and verifies a unit of behavior, which may span several classes. Immutable values can remain real in either approach. section 2.2 section 2.5

Prefer observable behavior over implementation details. Verifying that a command was called can couple a test to an intermediate interaction rather than the resulting behavior, making the test fragile under refactoring. Test doubles help with difficult dependencies, but they do not repair a design that is hard to test. section 5.5 section 2.5

Purchasing inventory

A classical test creates a real store, adds ten shampoos, and checks that purchasing five succeeds while inventory falls to five. A London-style test replaces the store with a mock, configures HasEnoughInventory to return true, and verifies that RemoveInventory is called once. For an insufficient-inventory case, it configures false and verifies that removal never occurs. The first emphasizes resulting state; the second emphasizes collaborator interaction.

How Unit Testing Principles, Practices, and Patterns covers it

How deep it goes
Defines the abstraction, classifies five forms into mocks and stubs, and compares isolation trade-offs without prescribing one universal style.
Examples
Uses inventory-purchase tests to contrast real dependencies, configured mocks, state assertions, and interaction verification.
What it assumes
Assumes object-oriented dependencies and unit-test isolation, while teaching the distinction between mutable collaborators and immutable values.
What you can do after
Apply it by isolating suitable dependencies while keeping assertions centered on observable behavior and refactoring resistance.
How it connects
Connects test doubles to unit testing, mocks, stubs, immutable values, observable behavior, implementation details, and integration testing.

Also explained in

Compare how 6 books explain test double →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Unit Testing Principles, Practices, and Patterns. The section links point to the parts of the book it draws on.