Spring Certified Professional 2023 · Free Practice Question Medium
Question 28
@WebMvcTest annotation will auto-configure: (select 3)
-
A
WebMvcConfigurerimplemented class -
B
MockMvc -
C
TestRestTemplate -
D
@ControllerAdviceannotated class
Reveal correct answers
Correct answers: A, B, D
Explanation
Javadoc:
Using this annotation will disable full auto-configuration and instead apply only configuration relevant to MVC tests (i.e. @Controller, @ControllerAdvice, @JsonComponent, Converter/GenericConverter, Filter, WebMvcConfigurer and HandlerMethodArgumentResolver beans but not @Component, @Service or @Repository beans).
By default, tests annotated with @WebMvcTest will also auto-configure Spring Security and MockMvc.
You should use @WebMvcTest annotation when you want to write an Integration Test focused on your application's web layer. @WebMvcTest approach will create ApplicationContext that contains only web components and omits any other components that are not part of the web layer. If required for the test, other components can be mocked with the usage of @MockBean annotation or delivered by @Configuration an annotated class imported with the usage of @Import annotation.
@WebMvcTest supports two cases:
Single Controller Auto-Configuration – annotate test by providing Controller class -
@WebMvcTest(CityController.class)

Multiple (All found) Controllers Auto-Configuration – annotate test with
@WebMvcTest

@WebMvcTest annotation will auto-configure:
Mock Mvc
@Controllerannotated class@ControllerAdviceannotated class@JsonComponentannotated classConverterannotated classGenericConverterannotated classFilterannotated classWebMvcConfigurerimplemented classHandlerMethodArgumentResolverannotated class
Spring doc:
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
