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.
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 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 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.
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.
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 Terms
Explore related glossary terms to deepen your understanding.
Related Resources
Dive deeper with these related interview prep pages.
Free QA career tools, no account needed
Instant and private, everything runs in your browser. Try them before you sign up.
QA Resume Checker
Instant 0-100 score on automation keywords, impact, and ATS formatting.
QA Cover Letter Generator
A tailored 3-paragraph QA cover letter from your resume and a job post.
QA Application Tracker
Drag-and-drop kanban to track every QA application from Applied to Offer.
QA Take-Home Test Generator
A realistic take-home assignment with a scenario, tasks, and a rubric.
QA LinkedIn Headline Generator
A recruiter-searchable headline, About section, and skills list.
QA STAR Story Builder
Structure a QA behavioral answer with the STAR method and instant checks.
QA Bug Report Generator
Build a clean, reproducible bug report for Markdown, Jira, or plain text.
Boundary Value Analysis Generator
Generate boundary value and equivalence partitioning test cases from a range.
QA Metrics Calculator
Calculate DRE, defect leakage, defect density, and pass rate with interpretation.
QA Test Plan Generator
Build a structured test plan (scope, approach, criteria, risks) in Markdown.
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