Software Testing & QA Medium technical 0 views 1 min read

What is the difference between mocks and stubs?

Peer-reviewed by HireXTech Technical Panel • Updated for 2025/2026 hiring • Editorial standards
Practise this track
Interviewer Expectations for this Question
01
Core Competency

Assesses fundamental understanding of Software Testing & QA conventions, runtime behavior, and memory/performance considerations.

02
Evaluation Criteria

Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.

Comprehensive Model Answer Verified Solution

Test doubles stand in for real collaborators. The common kinds differ in what they verify.

  • Dummy: passed but never used, just to satisfy a signature.
  • Stub: returns canned answers, for example a repository returning a fixed user.
  • Spy: a stub that also records how it was called.
  • Mock: pre-programmed with expectations and verified, so it asserts interaction.
  • Fake: a working but lightweight implementation, such as an in-memory database.
when(repo.find(1)).thenReturn(new User("a")); // stub
verify(email).send(any());                    // mock, checks interaction

Stubbing is about supplying indirect inputs; mocking is about verifying indirect outputs. Prefer stubs and fakes for state-based tests, because asserting on every call couples tests to implementation and makes refactoring painful. Use mocks when the interaction itself is the contract, such as an event being published exactly once. Overusing mocks produces tests that pass while the real integration is broken.

Candidate Response Strategy & Interview Tips

  1. Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
  2. Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
  3. Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
  4. Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.
Related Topics & Skills
Spotted an error or have an alternative solution?