@WebMvcTest annotation in Spring Boot?@WebMvcTest is used to test Spring MVC controllers. It loads only the web layer, not the full application context, making tests faster and focused on controller behavior.
@WebMvcTest?Only the web layer components like controllers, @ControllerAdvice, Filter, and WebMvcConfigurer are loaded. Services, repositories, and other beans are not loaded unless explicitly mocked.
@WebMvcTest controller test?You use @MockBean to create a mock of the service and inject it into the controller during the test.
MockMvc in @WebMvcTest?MockMvc allows you to perform HTTP requests to the controller and verify responses without starting a real server.
@WebMvcTest preferred over @SpringBootTest for controller tests?@WebMvcTest loads only the web layer, making tests faster and more focused. @SpringBootTest loads the full application context, which is slower and less focused for controller testing.
@WebMvcTest load for testing?@WebMvcTest loads only the web layer, focusing on controllers and related MVC components.
@WebMvcTest?@MockBean creates a mock of the service and injects it into the test context.
@WebMvcTest?MockMvc simulates HTTP requests to controllers without starting a server.
@WebMvcTest over @SpringBootTest for controller tests?@WebMvcTest loads only the web layer, so tests run faster and focus on controller logic.
@WebMvcTest?Services are not loaded automatically in @WebMvcTest. You must mock them with @MockBean.
@WebMvcTest helps in testing Spring Boot controllers and what you need to do to handle service dependencies.@WebMvcTest and @SpringBootTest when testing controllers.