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

Java's static and final methods and classes are notoriously hard to mock with a plain mocking framework. Explain why, and describe at least two approaches to work around it: a design-level fix and a tooling-level fix. Recommend best practices and the trade-offs each approach has for maintainability and test reliability.

MediumTechnical
61 practiced

Describe strategies for testing asynchronous, event-driven services that communicate through a message broker. Compare mocking at the producer/consumer boundary against running an embedded or lightweight broker in tests, and give sample patterns for verifying message contents, ordering guarantees, and side effects.

HardTechnical
52 practiced

Explain how threads, async/await, and separate processes each affect test isolation, resource usage, and the risk of nondeterministic behavior in a test runner. What would you recommend for designing a parallel test runner that avoids shared-state issues and memory leaks, including how you would scope fixtures and decide which tests are safe to parallelize versus which need to stay serial?

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

Unlock Full Question Bank

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

Sign in to Continue

Join thousands of developers preparing for their dream job.