What is Mocking vs Stubbing?
Mocking and stubbing are both techniques for replacing real dependencies with test doubles, but stubs provide canned responses to calls while mocks additionally verify that specific interactions occurred.
Free to start · 7-day trial on paid plans
In depth.
Gerard Meszaros defined five types of test doubles: dummies (never used, just fill parameters), fakes (working implementations with shortcuts, like an in-memory database), stubs (return pre-programmed responses), spies (record calls for later verification), and mocks (pre-programmed with expectations that are verified during the test). In practice, most teams use "mock" loosely to mean any test double.
Stubs are state-based: you set up a return value and assert on the outcome of the system under test. "When the payment gateway is called, return success. Then verify the order status is confirmed." Mocks are interaction-based: you assert that a specific method was called with specific arguments. "Verify that the email service was called exactly once with the user's email and the order confirmation template."
Over-mocking is the most common pitfall. When tests mock every dependency, they become coupled to implementation details rather than behavior. A refactor that changes internal method calls without changing behavior breaks all the mocks. The guideline is: mock at architectural boundaries (APIs, databases, third-party services) and test internal logic with real objects whenever possible.
Why interviewers ask about this.
This is a frequent SDET interview question. Interviewers want to see that you understand when to use each type of test double and the trade-offs of over-mocking versus under-mocking.
Example scenario.
An order service depends on an inventory API and an email service. In a unit test, the inventory API is stubbed to return "in stock" (we care about the response, not that it was called). The email service is mocked to verify it was called with the correct template and recipient after a successful order.
Interview tip.
Reference Meszaros' test-double taxonomy. Give a concrete example showing when you would use a stub (state verification) versus a mock (behavior verification). Mention the danger of over-mocking.
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.
QA Salary Calculator
Estimate QA, SDET, and automation tester pay by level, market, and skills.
QA Offer Evaluator
See total comp, a counter range, and a ready-to-send negotiation message.
Ready to Ace Your QA Interview?
Practice explaining mocking vs stubbing and other key concepts with our AI interviewer.
Join 1,200+ QA engineers already practicing with AssertHired.
Start your free QA interviewCurious how it works first? See the QA mock interview