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
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
