Scrivio Interview library

Mockito and test doubles

24 questions in 7 subtopics.

Mocks vs stubs vs spies

ConceptFoundations6 min read
Explain the difference between a stub, a mock, a spy and a fake, with a Java example of each.
DebuggingFoundations6 min read
This spy test fails with an exception from the real method:
List<String> spy = spy(new ArrayList<>());
when(spy.get(0)).thenReturn("x");
Why, and how do you fix it?
DesignIntermediate6 min read
When would you prefer a fake (for example an in-memory repository) over Mockito mocks in service tests?
ScenarioAdvanced6 min read
In Spring Boot tests teams use @MockitoBean heavily, and the suite starts dozens of application contexts. Explain why and what you would change.

Argument matchers and captors

CodingFoundations6 min read
Show how to use an ArgumentCaptor to check the email address passed to emailSender.send(Email email) inside a service under test.
DebuggingFoundations6 min read
Why does this
throw InvalidUseOfMatchersException? when(repo.find(anyString(), 10)).thenReturn(user);
ConceptIntermediate6 min read
When is argThat with a custom matcher better than a captor, and what are the trade-offs?

Mocking static and final

CodingFoundations6 min read
How do you mock a static method such as LocalDate.now() with Mockito, and why is it often a sign to change the design?
InternalsIntermediate7 min read
How does Mockito mock final classes and static methods, and what changed with recent JDKs regarding agent loading?
ScenarioPrincipal6 min read
Mockito's inline mocking relies on agent-based bytecode changes, and the JDK is restricting dynamic agent loading. What long-term risk does this create for a large test estate, and how would you reduce it?

Verification

ConceptFoundations5 min read
What do verify(mock, times(2)), never(), atLeastOnce() and inOrder check in Mockito? When should you avoid verifyNoMoreInteractions?
DebuggingIntermediate6 min read
A test using MockitoExtension fails with UnnecessaryStubbingException after a refactor. What does it mean and how should you respond?
DesignAdvanced6 min read
Write a test standard for your organisation on when to use verify() in Mockito, so that tests stay useful during refactoring.
LeadershipPrincipal6 min read
Over many years, how has the use of mocking frameworks shaped Java code design, for better and worse, and what guidance would you give a large organisation now?

Over-mocking

ConceptFoundations6 min read
What does 'do not mock types you do not own' mean, using a Spring RestClient or JDBC call as an example?
ScenarioIntermediate6 min read
A pull request adds tests that mock every collaborator, including value objects and the repository, and verify every call. How would you review it?
LeadershipAdvanced6 min read
Your organisation's test suites have high coverage but production defects remain frequent and refactors break hundreds of tests. What test standards would you introduce?
LeadershipPrincipal6 min read
You are setting a ten-year test architecture direction. Some teams argue for mocking-heavy unit tests, others for real infrastructure via containers. How do you decide and what do you write down?

Mockito annotations and injection

CodingFoundations6 min read
Rewrite this test setup with Mockito annotations and the JUnit 5 extension, and explain what each annotation does:
OrderRepository repo = mock(OrderRepository.class);
EmailSender email = mock(EmailSender.class);
OrderService service = new OrderService(repo, email);
ArgumentCaptor<Email> captor = ArgumentCaptor.forClass(Email.class);
DebuggingIntermediate7 min read
A test uses @InjectMocks PaymentService service with two @Mock fields of the same type, PaymentGateway primary and PaymentGateway fallback. The test passes, but the service calls the wrong gateway, and another test shows a mock as null inside the service. Explain how @InjectMocks chooses and fails, and how you would fix the tests.
LeadershipAdvanced7 min read
Test code across your organisation mixes @InjectMocks, @MockitoBean in full Spring contexts, and hand-written constructors, and test suites take 40 minutes partly because of many distinct Spring contexts. What testing conventions would you set?

EasyMock and other mocking libraries

ConceptFoundations6 min read
Compare Mockito with EasyMock, JMockit, MockK and Spock mocks. What is the record-replay-verify model in EasyMock, and why do most Java teams use Mockito today?
ScenarioIntermediate6 min read
A legacy project has 6,000 tests using JMockit and EasyMock, and the upgrade to Java 25 breaks JMockit. How would you plan the migration to Mockito with minimal disruption?
LeadershipPrincipal6 min read
Testing libraries come and go: JMock, EasyMock, PowerMock and JMockit were once popular and are now legacy or abandoned. How would you choose and govern test frameworks so the company is not left with thousands of tests on a dead library again?