InterviewStack.io LogoInterviewStack.io

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.

EasyTechnical
47 practiced

In Python, you have a function fetch_user(user_id) that calls requests.get(...) against a live API and returns parsed JSON. Write a unit test using unittest and unittest.mock that stubs the call so the test runs entirely offline, and assert both the returned data and that the call was made with the expected URL.

MediumTechnical
48 practiced

In Java, using JUnit and Mockito, write a unit test for a Service.processOrder(order) method that is expected to charge a payment gateway, save the order via a repository, and publish an order-placed event. Verify the call order and the arguments passed to each collaborator, and add a test for the case where the payment call throws: the order must not be saved or published.

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?

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

Design tests that simulate partial failures across a distributed transaction implemented as a saga, using mocks or service virtualization for the participating services. The tests must validate idempotency, compensating actions, and correct recovery if the orchestrating process restarts mid-saga. Describe concrete test scenarios and how you would automate them safely in CI.

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.