Spring Boot - Testing Spring Boot Applications
Consider this test class:
@WebMvcTest(controllers = ProductController.class)
class ProductControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private ProductService productService;
@Test
void testGetProduct() throws Exception {
when(productService.getProduct(1L)).thenReturn(new Product(1L, "Book"));
mockMvc.perform(get("/products/1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value("Book"));
}
}
What is the purpose of the @MockBean annotation here?