Consider this Spring Boot test class using @MockBean to mock a service. What will be the printed output when the test runs?
import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; import static org.mockito.BDDMockito.given; @SpringBootTest class UserServiceTest { @MockBean private UserRepository userRepository; @Autowired private UserService userService; @Test void testGetUserName() { given(userRepository.findNameById(1L)).willReturn("Alice"); System.out.println(userService.getUserName(1L)); } } @Service class UserService { @Autowired private UserRepository userRepository; public String getUserName(Long id) { return userRepository.findNameById(id); } } interface UserRepository { String findNameById(Long id); }
Think about what @MockBean does and how given(...).willReturn(...) affects the mock.
The @MockBean annotation creates a mock of UserRepository and injects it into the Spring context. The given(...).willReturn("Alice") sets the mock to return "Alice" when findNameById(1L) is called. So, the userService.getUserName(1L) call prints "Alice".
You have a @MockBean for PaymentService. Which assertion correctly verifies that processPayment() was called exactly once?
import static org.mockito.Mockito.*; @MockBean PaymentService paymentService; // In test method: paymentService.processPayment(100);
Remember how Mockito verifies method calls on mocks.
The verify() method with times(1) checks that processPayment(100) was called exactly once. Option A does not specify times(1), so it passes if called any number of times. Options A and B are incorrect because they assert return values, not method calls.
Given this test, why does it throw a NullPointerException?
import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; import static org.mockito.BDDMockito.given; @SpringBootTest class OrderServiceTest { @MockBean private PaymentService paymentService; private OrderService orderService = new OrderService(); @Test void testOrder() { given(paymentService.pay(50)).willReturn(true); boolean result = orderService.placeOrder(50); System.out.println(result); } } class OrderService { private PaymentService paymentService; public boolean placeOrder(int amount) { return paymentService.pay(amount); } } interface PaymentService { boolean pay(int amount); }
Check how the OrderService instance is created and how dependencies are injected.
The OrderService instance is created manually with new and not managed by Spring. Therefore, its paymentService field is null. The @MockBean creates a mock and injects it into Spring-managed beans only. So calling paymentService.pay() causes NullPointerException.
Choose the best explanation of the difference between @Mock and @MockBean in Spring Boot testing.
Think about how Spring manages beans and how mocks are injected.
@Mock creates a mock object but does not integrate with Spring's application context. @MockBean creates a mock and replaces the bean in the Spring context, so Spring injects the mock wherever needed.
You have nested test classes inside a Spring Boot test. Which configuration ensures @MockBean mocks are injected correctly in all nested classes?
Consider how Spring manages context and test instance lifecycle.
Annotating the outer class with @SpringBootTest loads the Spring context once. Using @TestInstance(Lifecycle.PER_CLASS) allows nested test classes to share the same test instance and context, so @MockBean mocks are injected properly in nested classes.