Back to the glossary

Test levels and methods

Test double

A test double is a stand-in that takes the place of a real dependency in a test, such as a database, a network service or a hardware driver. Well-known forms are mocks, stubs and fakes.

Also known as: mock, stub, fake

Why replace dependencies?

Real dependencies make tests slow, unreliable or simply impossible: the database isn’t reachable, the external service charges per call, the hardware sits in the lab. A test double replaces them with something simple and controllable.

The common kinds

  • Stub: returns fixed answers, for example always 23 °C for a temperature sensor
  • Mock: additionally checks whether and how it was called
  • Fake: a working but simplified implementation, such as an in-memory database
  • Spy: records calls made to a real object

The limits

A test double is an assumption about how the real dependency behaves. If the assumption is wrong, the tests pass anyway. That’s why unit tests with doubles should always be complemented by integration tests against the real components.

In embedded work the principle is the same, it just goes by other names: a hardware abstraction layer with simulated drivers makes software-in-the-loop possible.