liveBook · Manning

Test double

As explained in Microservices Patterns, Second Edition

A test double is a substitute component that simulates the behavior of a real collaborator or dependency during testing.

A test double substitutes for a real collaborator and simulates its behavior during a test. Replacing a troublesome dependency can simplify automated tests and let a component be tested in isolation. section 5.4.3 section 7.3.1

Dependency injection allows a test to supply the substitute to the component under test. For example, a mock data access object can simulate database access, enabling a fast in-memory unit test instead of a test that requires a database. section 5.4.3 section 5.4.4

The same approach supports local development and component-level pipelines. A developer can run one component locally while simulating its service dependencies; in a modular monolith, module tests can substitute for other domain modules. section 5.4.5 section 6.2.5

Testing an Order Service locally

A developer runs the Order Service with its required infrastructure, such as a database and message broker, while a test double simulates the Customer Service. This avoids running every service in the application. section 5.4.5

Isolation is useful only if the substitute represents the collaborator’s API accurately. Simple, well-defined, stable APIs make collaborators easier to simulate; contract tests help verify that services implement and use the APIs expected by their clients and collaborators. section 7.3.1

Use doubles where dependencies make tests slow or difficult, but do not assume simulation replaces every real interaction. For example, testing actual third-party webhook requests may require a remote environment. Side-effect-free functions with no dependencies offer another way to simplify tests without configuring doubles. section 5.4.5 section 5.4.3

How Microservices Patterns, Second Edition covers it

How deep it goes
Explains dependency injection and isolation, but defers consumer-driven contract testing and does not detail double implementation.
Examples
Illustrates a mock DAO, simulated service dependencies, and module tests that substitute for other domain modules.
What you can do after
Evaluate when to replace dependencies and when real interactions or contract tests remain necessary.
How it connects
Connects test doubles to dependency injection, unit testing, modular monoliths, microservices, and contract testing.

Also explained in

Compare how 9 books explain test double →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Microservices Patterns, Second Edition. The section links point to the parts of the book it draws on.