@WebMvcTest helps you test only the web layer of your Spring Boot app without starting the full application. It makes controller testing faster and focused.
0
0
@WebMvcTest for controller testing in Spring Boot
Introduction
You want to check if your controller handles HTTP requests correctly.
You want to test controller methods without loading services or repositories.
You want to verify the JSON response or view returned by a controller.
You want to test request mappings and status codes quickly.
You want to isolate controller logic from other layers during testing.
Syntax
Spring Boot
@WebMvcTest(ControllerClass.class) public class YourControllerTest { @Autowired private MockMvc mockMvc; // test methods here }
Use @WebMvcTest with the controller class you want to test.
MockMvc is used to simulate HTTP requests and check responses.
Examples
Basic test for a controller that returns "Hello World" on /hello.
Spring Boot
@WebMvcTest(HelloController.class) public class HelloControllerTest { @Autowired private MockMvc mockMvc; @Test public void testHello() throws Exception { mockMvc.perform(get("/hello")) .andExpect(status().isOk()) .andExpect(content().string("Hello World")); } }
If you omit the controller class, Spring loads all controllers for testing.
Spring Boot
@WebMvcTest public class MultipleControllersTest { @Autowired private MockMvc mockMvc; // Tests for multiple controllers if no specific class is given }
Use @MockBean to mock dependencies like repositories when testing controllers.
Spring Boot
@WebMvcTest(UserController.class) @Import(UserService.class) // Import service if needed public class UserControllerTest { @Autowired private MockMvc mockMvc; @MockBean private UserRepository userRepository; // Test methods }
Sample Program
This example tests the GreetingController's /greet endpoint. It mocks GreetingService to return "Hello Test" instead of the real value. The test checks that the controller returns the mocked string with HTTP 200 status.
Spring Boot
package com.example.demo; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.web.servlet.MockMvc; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @WebMvcTest(GreetingController.class) public class GreetingControllerTest { @Autowired private MockMvc mockMvc; @MockBean private GreetingService greetingService; @Test public void testGreeting() throws Exception { // Mock the service to return "Hello Test" org.mockito.Mockito.when(greetingService.greet()).thenReturn("Hello Test"); mockMvc.perform(get("/greet")) .andExpect(status().isOk()) .andExpect(content().string("Hello Test")); } } // Controller class package com.example.demo; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class GreetingController { private final GreetingService greetingService; public GreetingController(GreetingService greetingService) { this.greetingService = greetingService; } @GetMapping("/greet") public String greet() { return greetingService.greet(); } } // Service class package com.example.demo; import org.springframework.stereotype.Service; @Service public class GreetingService { public String greet() { return "Hello World"; } }
OutputSuccess
Important Notes
@WebMvcTest loads only web-related beans, so other beans must be mocked or imported.
Use @MockBean to create mocks for dependencies your controller needs.
MockMvc lets you simulate HTTP requests without starting a server.
Summary
@WebMvcTest is for focused testing of Spring MVC controllers.
It speeds up tests by loading only web layer components.
Use MockMvc and @MockBean to test controller behavior and dependencies.