What is the difference between mocks and stubs?
Assesses fundamental understanding of Software Testing & QA conventions, runtime behavior, and memory/performance considerations.
Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.
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
- Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
- Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
- Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
- Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.