Ch. 22 · Software Testing

Mocks, Stubs and Fakes: Dependency Test Choices

Mocks, Stubs and Fakes: Dependency Test Choices. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readintermediateupdated Oct 3, 2026

Test doubles provide controlled dependencies with different purposes. Choose the simplest double that exposes the relevant behavior and contract.

Before you start

You should understand inputs, expected outputs and the boundaries of the component under test. State the risk a test should detect before choosing a tool or mock. Distinguish controlled dependencies from real integrations so the result does not imply more coverage than the test actually provides.

The practical goal is to reason through this situation: A fake repository stores records; a stub returns a fixed failure; a mock verifies a required interaction. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.

Step-by-step walkthrough

Step 1: Choose needed control

A fixed failure stub differs from a working in-memory fake.

Step 2: Assert meaningful behavior

Verify order state and permitted retry rather than every internal call.

Step 3: Validate the real contract separately

A permissive double can conceal an invalid integration request.

Worked scenario

A fake repository stores records; a stub returns a fixed failure; a mock verifies a required interaction.

A fake payment adapter accepts every object, so tests pass even when the service sends an unsupported currency field. A contract or integration check against the real interface detects that mismatch. Keep the local failure test focused on domain recovery while documenting which external behavior its double does not establish.

Common mistake

A double that behaves unlike the real dependency can create false confidence.

Verify the behavior

Test failure recovery locally and request compatibility at the actual integration boundary.

Interview exercise

Test payment failure.

Answer and reasoning

Control the failure outcome and assert order state and retry behavior, then separately verify the real gateway contract.

Continue learning

Compare the scenario with the Software Testing interview questions and test your understanding with the Software Testing MCQs. For terminology and implementation details, consult the reference material.

More in Software Testing

read ✓Software Testing · hard

Testing Time with an Injected Clock

Inject a clock instead of calling the system time, freeze it in tests, and remove the sleeps that make tests flaky.

~2 min readread →
read ✓Software Testing · mid

Code Coverage and Its Limits

Read line and branch coverage as a signal, not a goal, and avoid the tests that chase the number without checking behavior.

~2 min readread →
read ✓Software Testing · hard

Scoping End-to-End Tests

Reserve end-to-end tests for critical user journeys, use stable selectors, and avoid duplicating coverage the lower tests already have.

~2 min readread →
esc