Testing Controllers with MockMvc
Testing Spring Boot Controllers with MockMvc
Testing controllers is essential to ensure your application's endpoints work as expected. In Spring Boot, you can use the MockMvc tool to test controllers without starting the full server. This approach is fast, reliable, and lets you focus on controller logic.
What is MockMvc?
MockMvc is a Spring testing utility that allows you to simulate HTTP requests and assert responses. It works directly with your controller layer, so you can test endpoints just like a real client would, but without running the application server.
Setting Up a Simple Controller
Suppose you have a controller like this:
@RestController
public class GreetingController {
@GetMapping("/greet")
public String greet() {
return "Hello, World!";
}
}
Writing a MockMvc Test
You can write a test for this controller using MockMvc as follows:
@WebMvcTest(GreetingController.class)
public class GreetingControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testGreetEndpoint() throws Exception {
mockMvc.perform(get("/greet"))
.andExpect(status().isOk())
.andExpect(content().string("Hello, World!"));
}
}
Step-by-Step Explanation
- Use the
@WebMvcTestannotation to load only the controller layer for testing; - Inject the
MockMvcobject with@Autowired; - Call the
perform()method with an HTTP GET request to/greet; - Chain assertions:
andExpect(status().isOk())checks that the response status is 200 OK;andExpect(content().string("Hello, World!"))checks that the response body matches the expected string.
Why Use MockMvc?
- Simulates real HTTP requests without running a server;
- Keeps tests fast and focused on controller behavior;
- Lets you check both status codes and response content.
Use MockMvc whenever you want to test your Spring Boot controllers quickly and reliably.
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Can you show how to test controllers with request parameters or path variables?
How do I test POST requests with MockMvc?
What should I do if my controller uses services or dependencies?
Fantastico!
Completion tasso migliorato a 8.33
Testing Controllers with MockMvc
Scorri per mostrare il menu
Testing Spring Boot Controllers with MockMvc
Testing controllers is essential to ensure your application's endpoints work as expected. In Spring Boot, you can use the MockMvc tool to test controllers without starting the full server. This approach is fast, reliable, and lets you focus on controller logic.
What is MockMvc?
MockMvc is a Spring testing utility that allows you to simulate HTTP requests and assert responses. It works directly with your controller layer, so you can test endpoints just like a real client would, but without running the application server.
Setting Up a Simple Controller
Suppose you have a controller like this:
@RestController
public class GreetingController {
@GetMapping("/greet")
public String greet() {
return "Hello, World!";
}
}
Writing a MockMvc Test
You can write a test for this controller using MockMvc as follows:
@WebMvcTest(GreetingController.class)
public class GreetingControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testGreetEndpoint() throws Exception {
mockMvc.perform(get("/greet"))
.andExpect(status().isOk())
.andExpect(content().string("Hello, World!"));
}
}
Step-by-Step Explanation
- Use the
@WebMvcTestannotation to load only the controller layer for testing; - Inject the
MockMvcobject with@Autowired; - Call the
perform()method with an HTTP GET request to/greet; - Chain assertions:
andExpect(status().isOk())checks that the response status is 200 OK;andExpect(content().string("Hello, World!"))checks that the response body matches the expected string.
Why Use MockMvc?
- Simulates real HTTP requests without running a server;
- Keeps tests fast and focused on controller behavior;
- Lets you check both status codes and response content.
Use MockMvc whenever you want to test your Spring Boot controllers quickly and reliably.
Grazie per i tuoi commenti!