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.

HardTechnical
59 practiced

Your CI pipeline is unstable because several tests depend on flaky or slow third-party services. Propose a hybrid strategy that decides, dependency by dependency, whether to mock it, virtualize it, or keep a small number of real integration tests. Give criteria for which tests should stay live, and explain how you would know if that mix is drifting the wrong way over time.

EasyTechnical
57 practiced

Describe test isolation in the context of automated testing for a microservice. Explain why isolation matters, list common sources of test interference, and outline the practices you would enforce in CI to keep tests isolated.

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
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
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.

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.