Test double
A test double is any object that stands in for a real dependency in a test — mock, stub, fake, spy, dummy. The 'double' umbrella term (Gerard Meszaros) avoids the precise-but-confusing distinctions between mock and stub; each variant has specific semantics about what it records and what it controls.
The five canonical doubles: dummy (passes through but isn't used), stub (returns canned responses, no assertions), fake (working implementation with shortcuts, e.g. in-memory database), spy (records calls for later assertion), and mock (pre-programmed with expectations, fails if not met). The choice matters: mocks couple tests to implementation; stubs and fakes couple tests to behaviour. Tests written with heavy mocking often break on refactors that don't change behaviour; tests written with stubs and fakes are more resilient. The pragmatic guidance: prefer the simplest double that meets the need (dummy < stub < fake < spy < mock).
Related terms
- Mock vs stub
A stub returns pre-defined responses to method calls without assertions; a mock additionally asserts that specific calls happened with specific arguments.
- Dependency injection
Dependency injection is the design pattern where a component receives its dependencies as parameters rather than constructing them internally.
- Test fixture
A test fixture is the prepared state required to run a test — input data, mocked dependencies, configured environment, seeded database rows.