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

Write a pytest unit test for a Python function schedule_next_run(last_run_timestamp) that returns the next UTC timestamp exactly 24 hours later. Use a time-freezing tool or monkeypatching to control system time so the test is deterministic, and include a test case that covers a daylight-saving-time boundary.

EasyTechnical
42 practiced

Give examples of using mocks or stubs to simulate exception and error conditions, such as a network timeout, a database constraint violation, or a message-broker disconnect. Explain why deliberately exercising these error paths in tests is valuable for product reliability, beyond just covering the happy path.

HardTechnical
50 practiced

Implement a deterministic test harness for asynchronous code that processes a queue of tasks in the background. Show how you would structure the tests to avoid flakiness by injecting or controlling the scheduler, and assert on the processing order of multiple queued tasks.

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.

MediumTechnical
50 practiced

You are writing integration tests for a service that depends on a rate-limited third-party API. Propose a checklist of practices to keep the tests stable and safe to run in CI without exceeding the provider's rate limits or causing real side effects. Explain which parts of your checklist rely on mocking versus other techniques, and how you would categorize existing tests by how much they depend on that external dependency.

Unlock Full Question Bank

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

Sign in to Continue

Join thousands of developers preparing for their dream job.