Mocking, Stubbing, and Test Isolation Questions
Techniques for isolating tests from external dependencies using mocks, stubs, and test doubles. Understanding when to mock vs. when to use real services, and how to make tests reliable while still validating real behavior.
MediumTechnical
48 practiced
In Java using JUnit and Mockito, write a unit test for a Service class method processOrder(order) that should: call PaymentGateway.charge(amount), save the Order via OrderRepository.save(order), and publish an OrderPlacedEvent via EventPublisher. Show how you would mock dependencies, verify call order and arguments (use ArgumentCaptor), and test the behavior when PaymentGateway throws an exception (order must not be saved nor published).
EasyTechnical
45 practiced
Explain how dependency injection and programming to interfaces improve testability. As an SDET propose a small, language-agnostic pattern (constructor injection, factory, or service locator) you would ask engineers to adopt to make services easier to mock and to support reliable unit tests.
EasyTechnical
42 practiced
Give examples and short pseudocode of using mocks or stubs to simulate exceptions and error conditions (for example: network timeout, database unique-constraint violation, message broker disconnect). Explain why exercising error paths is valuable for improving product reliability from an SDET perspective.
MediumTechnical
41 practiced
Explain strategies for mocking static or final methods and classes in Java unit tests. Discuss approaches: refactoring to interfaces, using Mockito inline/mock-maker-inline, PowerMock, or bytecode instrumentation. As an SDET recommend best practices and trade-offs related to maintainability and test reliability.
MediumTechnical
56 practiced
Discuss lifecycle management for test doubles in a large test suite: creation, configuration, verification, teardown, and reuse. As an SDET propose patterns and conventions (naming, fixture scope, reset semantics) to avoid leaking state, reduce duplication, and make mocks consistent across teams.
Unlock Full Question Bank
Get access to hundreds of Mocking, Stubbing, and Test Isolation interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.