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
42 practiced

Give a decision rubric for when to mock an external dependency (a third-party API, a database, a cache) versus calling the real thing in a test. Name the factors your rubric weighs, and explain how the right answer can differ between a local development run, a CI pipeline, and a staging environment.

MediumTechnical
56 practiced

Discuss lifecycle management for test doubles in a large test suite: how they get created and configured, how you verify they were used correctly, and how they get reset and torn down between tests. Propose naming and fixture-scope conventions to avoid leaking mock state between tests and keep mocks consistent across a team.

HardTechnical
57 practiced

You need to test payment flows that must validate idempotency and retry behavior, but you cannot call the production payment gateway from automated tests. Propose a strategy to mock or virtualize the gateway that preserves realistic behavior, including stateful idempotency tokens, duplicate-request detection, and injected errors. How would you verify that your mock is actually correct?

MediumTechnical
59 practiced

Explain the roles of unit, integration, and end-to-end tests in a delivery pipeline. For each layer, describe what should typically be mocked versus run against a real dependency, and walk through a concrete example for a payment flow showing where mocks belong and why.

MediumTechnical
58 practiced

Your team's multi-tenant test suite is riddled with flaky, order-dependent failures because tests leave behind data that later tests accidentally collide with or depend on. Design a fixture and isolation strategy that fixes this for good, and explain when you would reach for a mock or a virtualized dependency instead of a full end-to-end call as part of that design.

Unlock Full Question Bank

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

Sign in to Continue

Join thousands of developers preparing for their dream job.