Skip to main content
Automation

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.

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 Interview

Free to start · 7-day trial on paid plans

Written by Aston Cook, Senior QA EngineerLast updated: March 2026