Spring Certified Professional 2023 · Free Practice Question Medium

Question 28

@WebMvcTest annotation will auto-configure: (select 3)

  • A

    WebMvcConfigurer implemented class

  • B

    MockMvc

  • C

    TestRestTemplate

  • D

    @ControllerAdvice annotated 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.

https://docs.spring.io/spring-boot/api/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTest.html



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:

  1. Single Controller Auto-Configuration – annotate test by providing Controller class - @WebMvcTest(CityController.class)

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

@WebMvcTest annotation will auto-configure:

  • Mock Mvc

  • @Controller annotated class

  • @ControllerAdvice annotated class

  • @JsonComponent annotated class

  • Converter annotated class

  • GenericConverter annotated class

  • Filter annotated class

  • WebMvcConfigurer implemented class

  • HandlerMethodArgumentResolver annotated class


Spring doc:

https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTest.html

Discussion

Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need