Testing / JUnit6 Interview Questions
How does JUnit 6 interact with Mockito and Spring Test?
JUnit 6 integrates with major testing frameworks via its Extension Model. Mockito and Spring Test both provide JUnit 6-compatible extensions.
// Mockito with JUnit 6: // Use MockitoExtension (works identically to JUnit 5) @ExtendWith(MockitoExtension.class) class OrderServiceTest { @Mock OrderRepository repository; @InjectMocks OrderService service; @Test void createsOrder() { when(repository.save(any())).thenReturn(new Order("O-1", Status.PENDING)); Order order = service.create("item-1", 2); assertEquals(Status.PENDING, order.getStatus()); verify(repository).save(any(Order.class)); } } // Spring Boot Test with JUnit 6: @SpringBootTest class ApplicationIntegrationTest { // SpringExtension is auto-registered by @SpringBootTest @Autowired OrderService service; @Test void contextLoads() { assertNotNull(service); } } // Spring with Mockito beans: @SpringBootTest class OrderControllerTest { @Autowired MockMvc mockMvc; @MockBean // Spring-managed mock -- replaces real bean in context OrderRepository repo; @Test void getOrders() throws Exception { when(repo.findAll()).thenReturn(List.of(new Order("O-1", Status.PENDING))); mockMvc.perform(get("/orders")) .andExpect(status().isOk()) .andExpect(jsonPath("$[0].id").value("O-1")); } } // Dependency versions for JUnit 6 compatibility (mid-2026): // Mockito 5.x: compatible with JUnit 6 // Spring Boot 3.x: compatible with JUnit 6 // spring-boot-starter-test auto-includes junit-jupiter
More Related questions...