Cosmic Module
S
Qubits of DPK
April 8, 2026
Core SWE @ Google
In real-world software systems, components often depend on external systems such as databases, APIs, or network services.
Testing code that relies on these external dependencies can be difficult because those systems may be slow, unreliable, or unavailable during testing.
This chapter explains how engineers use test doubles to simulate these dependencies during testing.
The central idea of this chapter is:
Test doubles allow engineers to test code in isolation by replacing real dependencies with controlled substitutes.
By using test doubles, engineers can verify system behavior without relying on external systems.
What Are Test Doubles?
A test double is a substitute used in testing to replace a real component.
Instead of interacting with a real dependency, the code interacts with the test double.
This allows tests to run:
- faster
- more reliably
- without external dependencies
Test doubles are commonly used when testing components that depend on complex systems.
Why Test Doubles Are Necessary
Many systems interact with external services such as:
- databases
- web APIs
- file systems
- payment services
- authentication services
Using these real systems in tests can cause problems.
Test doubles solve these problems by replacing external systems with simplified versions.
Types of Test Doubles
There are several types of test doubles, each serving a different purpose.
Understanding these types helps engineers choose the appropriate test strategy.
Stubs
A stub provides predetermined responses when called during a test.
Example:
Suppose a function normally retrieves data from a database.
Instead of connecting to the real database, a stub may return predefined data.
Example stub behavior:
java
QUBITS OF DPK
This allows the test to verify how the code behaves when receiving specific data.
Mocks
Mocks are used to verify interactions between components.
They check whether certain functions were called during the test.
Example:
A system might send a notification when a payment is processed.
A mock object can verify whether the notification function was called.
Example verification:
java
QUBITS OF DPK
This ensures the correct interactions occurred.
Fakes
A fake is a simplified implementation of a real system.
Unlike stubs or mocks, fakes often contain working logic but operate in a simplified way.
Example:
Instead of using a real database, a test may use an in-memory database that stores data temporarily during the test.
Fakes allow more realistic testing without the complexity of real systems.
Choosing the Right Test Double
Different testing scenarios require different types of test doubles.
Engineers choose the most appropriate substitute based on the testing goal.
Benefits of Using Test Doubles
Test doubles provide several advantages.
These advantages make test doubles an essential tool in software testing.
Risks of Overusing Test Doubles
Although test doubles are useful, they should not completely replace real system testing.
Overusing test doubles can create tests that pass even though the real system would fail.
For example:
A mock database may behave differently from the real database.
Therefore, engineers should combine test doubles with integration and system tests.
Combining Testing Strategies
A balanced testing strategy includes:
- unit tests with test doubles
- integration tests with real components
- system tests verifying end-to-end behavior
This combination ensures both speed and accuracy in testing.
Test Doubles Improve Development Speed
When engineers use test doubles, they can run tests quickly during development.
Fast feedback allows engineers to:
- detect errors quickly
- experiment with changes safely
- improve code quality
This accelerates development without sacrificing reliability.
Key Lessons from Chapter 13
This chapter introduces several important principles.
- #Test doubles replace real dependencies during testing.
- #Stubs return predefined responses for tests.
- #Mocks verify interactions between components.
- #Fakes provide simplified working implementations.
- #Test doubles make tests faster and more reliable.
Simple Explanation (For Non-Technical Readers)
Imagine testing a new airplane simulator.
Instead of flying a real airplane every time you test something, engineers use a simulator that mimics the airplane's behavior.
The simulator allows engineers to test many scenarios safely and quickly.
Test doubles work in the same way.
They simulate real systems so that software can be tested efficiently without relying on complex external services.