Mocking, Stubbing, and Test Isolation Questions

Isolating the unit under test from its dependencies. Covers mocks, stubs, fakes, and spies, when to use test doubles versus real dependencies, and controlling external services and time. Includes designing for isolation so tests are fast, deterministic, and focused.

MediumTechnical
50 practiced

Compare the main strategies for virtualizing an external service in tests: hand-written contract-based stubs, a standalone mock server generated from an API contract, and network-level record-and-replay of real traffic. For each, discuss maintenance overhead, fidelity to production behavior, and how it scales when many tests run in CI. How would you decide it is safe to rely on one of these instead of the real dependency?

MediumTechnical
83 practiced

Implement a small Node.js mock server that returns a configurable response for GET /users/:id. Test code should be able to set the response payload and status for a given id at runtime through a control endpoint, and reset it between tests. Outline the code and explain how a test would use this server.

EasyTechnical
45 practiced

Explain how dependency injection and programming to interfaces improve testability. Propose a small, language-agnostic pattern (constructor injection, a factory, or a service locator) you would ask engineers to adopt so that their code becomes easier to mock and supports reliable unit tests.

HardTechnical
41 practiced

You need to introduce automated tests into a large legacy monolith with minimal refactoring risk. Describe the practical seams and techniques you would create to make the code mockable and testable, and give a step-by-step plan to increase coverage while minimizing risk to the existing behavior.

MediumTechnical
56 practiced

Explain mocking versus stubbing versus service virtualization for integration tests. For each technique, describe when you would use it, its benefits and limitations in terms of fidelity versus control, how it affects long-term test maintenance, and how you would fit it into a CI workflow for a backend dependency.

Unlock Full Question Bank

Get access to all 10 Mocking, Stubbing, and Test Isolation interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.