0
0
Spring Bootframework~30 mins

@WebMvcTest for controller testing in Spring Boot - Mini Project: Build & Apply

Choose your learning style9 modes available
Testing a Spring Boot Controller with @WebMvcTest
📖 Scenario: You are building a simple Spring Boot web application that has a controller to handle greeting requests. You want to write a test that checks if the controller returns the correct greeting message.
🎯 Goal: Create a Spring Boot test class using @WebMvcTest to test the GreetingController. You will set up the test class, configure the mock MVC, write a test method to perform a GET request, and verify the response.
📋 What You'll Learn
Create a controller class named GreetingController with a GET endpoint /greet that returns a greeting string.
Create a test class annotated with @WebMvcTest(GreetingController.class).
Inject a MockMvc object to perform HTTP requests in the test.
Write a test method that performs a GET request to /greet and asserts the response status is 200 and the content is the expected greeting.
💡 Why This Matters
🌍 Real World
Testing controllers with @WebMvcTest helps ensure your web endpoints behave correctly without starting the full application. This speeds up development and catches bugs early.
💼 Career
Many Java backend developer roles require writing unit and integration tests for Spring Boot applications. Knowing @WebMvcTest is essential for testing web layers efficiently.
Progress0 / 4 steps
1
Create the GreetingController class
Create a Spring Boot controller class named GreetingController with a method greet() mapped to GET requests at /greet. The method should return the string "Hello, Spring!".
Spring Boot
Need a hint?

Use @RestController on the class and @GetMapping("/greet") on the method.

2
Set up the test class with @WebMvcTest
Create a test class named GreetingControllerTest annotated with @WebMvcTest(GreetingController.class). Inside, declare a private field mockMvc of type MockMvc and annotate it with @Autowired.
Spring Boot
Need a hint?

Use @WebMvcTest on the test class and @Autowired to inject MockMvc.

3
Write a test method to perform GET request
In the GreetingControllerTest class, write a test method named testGreetReturnsHelloSpring annotated with @Test. Use mockMvc.perform(get("/greet")) to perform a GET request. Then use andExpect(status().isOk()) and andExpect(content().string("Hello, Spring!")) to verify the response status and content.
Spring Boot
Need a hint?

Use mockMvc.perform(get("/greet")) and chain andExpect calls to check status and content.

4
Complete the test class with imports and annotations
Ensure the test class GreetingControllerTest has all necessary imports: @Test from org.junit.jupiter.api.Test, static imports for get, status, and content from MockMvcRequestBuilders and MockMvcResultMatchers. Confirm the class is annotated with @WebMvcTest(GreetingController.class) and the mockMvc field is @Autowired.
Spring Boot
Need a hint?

Check that all imports and annotations are present for the test to run correctly.