Testing with @WebMvcTest
What is @WebMvcTest?
@WebMvcTest is a specialized annotation for testing only the web layer of your Spring application. It loads only the beans needed for controllers, filters, and related MVC components, making your tests faster and more focused.
Why use @WebMvcTest?
- Focuses on testing Spring MVC controllers;
- Does not load service, repository, or full application context beans;
- Allows you to verify request mappings, validation, and controller logic.
Example: Testing a Simple Controller
Suppose you have a simple controller:
@RestController
public class GreetingController {
@GetMapping("/greet")
public String greet() {
return "Hello, Spring!";
}
}
You can write a test for this controller using @WebMvcTest:
@WebMvcTest(GreetingController.class)
class GreetingControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
void testGreetEndpoint() throws Exception {
mockMvc.perform(get("/greet"))
.andExpect(status().isOk())
.andExpect(content().string("Hello, Spring!"));
}
}
Key Steps Explained
- Add
@WebMvcTest(GreetingController.class)to load only theGreetingControllerand necessary web components; - Inject
MockMvcto simulate HTTP requests and inspect responses without starting a real server; - Use
mockMvc.perform(get("/greet"))to send a GET request to the/greetendpoint; - Chain assertions to check the response status and body content.
Best Practices
- Use
@WebMvcTestfor controller logic, request mappings, and web validation; - Mock any dependencies your controller needs using
@MockBeanif required; - Avoid using
@WebMvcTestfor service or repository testing — use other test slices or full integration tests for those layers.
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Fantastiskt!
Completion betyg förbättrat till 8.33
Testing with @WebMvcTest
Svep för att visa menyn
What is @WebMvcTest?
@WebMvcTest is a specialized annotation for testing only the web layer of your Spring application. It loads only the beans needed for controllers, filters, and related MVC components, making your tests faster and more focused.
Why use @WebMvcTest?
- Focuses on testing Spring MVC controllers;
- Does not load service, repository, or full application context beans;
- Allows you to verify request mappings, validation, and controller logic.
Example: Testing a Simple Controller
Suppose you have a simple controller:
@RestController
public class GreetingController {
@GetMapping("/greet")
public String greet() {
return "Hello, Spring!";
}
}
You can write a test for this controller using @WebMvcTest:
@WebMvcTest(GreetingController.class)
class GreetingControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
void testGreetEndpoint() throws Exception {
mockMvc.perform(get("/greet"))
.andExpect(status().isOk())
.andExpect(content().string("Hello, Spring!"));
}
}
Key Steps Explained
- Add
@WebMvcTest(GreetingController.class)to load only theGreetingControllerand necessary web components; - Inject
MockMvcto simulate HTTP requests and inspect responses without starting a real server; - Use
mockMvc.perform(get("/greet"))to send a GET request to the/greetendpoint; - Chain assertions to check the response status and body content.
Best Practices
- Use
@WebMvcTestfor controller logic, request mappings, and web validation; - Mock any dependencies your controller needs using
@MockBeanif required; - Avoid using
@WebMvcTestfor service or repository testing — use other test slices or full integration tests for those layers.
Tack för dina kommentarer!