Mockito and test doubles
24 questions in 7 subtopics.
Mocks vs stubs vs spies
Explain the difference between a stub, a mock, a spy and a fake, with a Java example of each.This spy test fails with an exception from the real method:List<String> spy = spy(new ArrayList<>());
when(spy.get(0)).thenReturn("x");
@MockitoBean heavily, and the suite starts dozens of application contexts. Explain why and what you would change.Argument matchers and captors
Show how to use anArgumentCaptor to check the email address passed to emailSender.send(Email email) inside a service under test.Why does thisthrow InvalidUseOfMatchersException? when(repo.find(anyString(), 10)).thenReturn(user);
argThat with a custom matcher better than a captor, and what are the trade-offs?Mocking static and final
How do you mock a static method such asLocalDate.now() with Mockito, and why is it often a sign to change the design?How does Mockito mock final classes and static methods, and what changed with recent JDKs regarding agent loading?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
What doverify(mock, times(2)), never(), atLeastOnce() and inOrder check in Mockito? When should you avoid verifyNoMoreInteractions?A test using MockitoExtension fails with UnnecessaryStubbingException after a refactor. What does it mean and how should you respond?Write a test standard for your organisation on when to use verify() in Mockito, so that tests stay useful during refactoring.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
What does 'do not mock types you do not own' mean, using a SpringRestClient or JDBC call as an example?A pull request adds tests that mock every collaborator, including value objects and the repository, and verify every call. How would you review it?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?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
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);
@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.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
Compare Mockito withEasyMock, JMockit, MockK and Spock mocks. What is the record-replay-verify model in EasyMock, and why do most Java teams use Mockito today?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?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?No question matches these choices.