Skip to main content
Automation
DEFINITION

What is Test Double?

A test double is any object that stands in for a real dependency during testing, replacing a database, service, or collaborator with a controllable substitute so a test can run in isolation, deterministically, and fast.

Free to start · 7-day trial on paid plans

IN DEPTH

In depth.

Test double is the umbrella term (coined by Gerard Meszaros) for the family of stand-ins testers use to isolate the code under test from its real dependencies. Rather than calling a real database or payment service, you inject a double you control, making tests faster, deterministic, and focused on one unit of behavior.

There are five commonly cited types. A dummy is passed around but never actually used (it just fills a parameter). A stub returns canned answers to calls, used to drive the code down a particular path. A fake has a working but simplified implementation (for example, an in-memory database) suitable for tests but not production. A spy records how it was called so you can assert on the interactions afterward. A mock is pre-programmed with expectations and verifies that the right calls happened, often failing the test itself if they did not.

The practical distinction interviewers probe is stub vs mock: stubs provide state (return values you assert on), while mocks verify behavior (interactions you expect). Overusing mocks couples tests to implementation details and makes them brittle, so good engineers reach for the lightest double that does the job.

WHY IT MATTERS

Why interviewers ask about this.

Mocks vs stubs (and the broader test-double vocabulary) is a staple of unit-testing interviews. Using the terms precisely, and knowing when to verify behavior with a mock versus provide state with a stub, signals real testing maturity, not just buzzword familiarity.

EXAMPLE

Example scenario.

Testing an order service that calls a payment gateway, you replace the gateway with a stub that returns "approved" to test the success path and "declined" to test the failure path, no real charges, no network. To assert the service actually called the gateway exactly once with the right amount, you use a mock or spy and verify the interaction.

TIP

Interview tip.

Define test double as the umbrella term for stand-in dependencies, then list the five types (dummy, stub, fake, spy, mock). Nail the key distinction: stubs provide canned state you assert on; mocks verify expected interactions. Add that over-mocking makes tests brittle.

FAQ

Frequently asked questions.

What are the five types of test doubles?

Dummy (passed but unused), stub (returns canned answers to drive a path), fake (a simplified working implementation like an in-memory DB), spy (records calls so you can assert on them), and mock (pre-programmed with expectations that it verifies). They differ in whether they provide state or verify behavior.

What is the difference between a mock and a stub?

A stub provides canned return values so the code under test can proceed, you assert on the resulting state. A mock is set up with expectations about how it should be called and verifies those interactions, often failing the test itself if the expected calls did not happen. Stubs check state; mocks check behavior.

Related Resources

Dive deeper with these related interview prep pages.

FREE TOOLS  /  no signup

Free QA career tools, no account needed

Instant and private, everything runs in your browser. Try them before you sign up.

EXEC.NOW

Ready to Ace Your QA Interview?

Practice explaining test double and other key concepts with our AI interviewer.

Join 1,200+ QA engineers already practicing with AssertHired.

Start your free QA interview
FREE.TO.START  ·  7.DAY.TRIAL ON PAID PLANS
Written by Aston Cook, Senior QA EngineerLast updated May 2026