Complete the code to annotate the test class for controller testing with Spring Boot.
@[1](MyController.class) public class MyControllerTest { // test methods }
The @WebMvcTest annotation is used to test Spring MVC controllers specifically, loading only the web layer.
Complete the code to inject the MockMvc object for performing HTTP requests in the test.
@Autowired
private [1] mockMvc;MockMvc is the class used to simulate HTTP requests in Spring MVC tests.
Fix the error in the test method to perform a GET request and expect HTTP 200 OK status.
mockMvc.perform(get("/api/items")) .andExpect([1].status().isOk());
MockMvcResultMatchers provides methods to assert the response status and content.
Fill both blanks to mock a service bean and inject it into the test context.
@[1]
private MyService myService;@MockBean creates a mock of the service and adds it to the Spring context for the test.
Fill all three blanks to perform a POST request with JSON content and expect a JSON response.
mockMvc.perform(post("/api/items") .contentType(MediaType.[1]) .content("{\"name\":\"item1\"}")) .andExpect(content().contentType(MediaType.[2])) .andExpect(status().[3]());
Use MediaType.APPLICATION_JSON for JSON content type. The expected status is HTTP 200 OK with isOk().