@WebMvcTest on a controller class, which parts of the Spring application context are loaded for the test?@WebMvcTest loads only the web layer components such as controllers and MVC infrastructure. It does not load services or repositories, so tests run faster and focus on controller behavior.
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.web.servlet.MockMvc; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.junit.jupiter.api.Test; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @WebMvcTest(HelloController.class) class HelloControllerTest { @Autowired private MockMvc mockMvc; @Test void testHello() throws Exception { mockMvc.perform(get("/hello")) .andExpect(status().isOk()); } } @RestController class HelloController { @GetMapping("/hello") public String sayHello() { return "Hello!"; } }
The @GetMapping("/hello") method returns a string and is reachable. The test expects status().isOk() which means HTTP 200. So the test will pass with status 200 OK.
UserController and OrderController, using @WebMvcTest. Which annotation usage is correct?The controllers attribute expects an array of classes. The correct syntax uses curly braces {} to list multiple controllers.
NoSuchBeanDefinitionException for UserService. Why?import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.web.servlet.MockMvc; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.junit.jupiter.api.Test; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @WebMvcTest(UserController.class) class UserControllerTest { @Autowired private MockMvc mockMvc; @Test void testUser() throws Exception { mockMvc.perform(get("/user")) .andExpect(status().isOk()); } } @RestController class UserController { private final UserService userService; public UserController(UserService userService) { this.userService = userService; } @GetMapping("/user") public String getUser() { return userService.getName(); } } interface UserService { String getName(); }
@WebMvcTest loads only web layer beans. Service beans like UserService are not loaded unless you mock them with @MockBean. Without mocking, Spring cannot find UserService and throws NoSuchBeanDefinitionException.
@WebMvcTest instead of @SpringBootTest when testing controllers?@WebMvcTest loads only the web layer components, which makes tests faster and focused on controller behavior. @SpringBootTest loads the full application context, which is slower and used for integration tests.