Search code examples
spring-boot

SpringBoot @WebMvcTest enable AutoConfiguration for @ControllerAdive


I wrote my own @RestControllerAdvice bean and wanted it to be reusable through my projects.
Thus, I wrote my own @AutoConfiguration to set it up, which works like a charm - except for @WebMvcTests where it is not autoconfigured.
I understand that @WebMvcTest does not autoconfigure everything but now I wonder how to set it up properly (without @Autowiring it in every MvcTest).


Solution

  • Found this related question that showed me the way.

    You can add autoconfigurations that should be eligible for WebMvcTest bei using spring factories. For instance, see the spring-boot-test-autoconfigure repo.

    In my case, add a file org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc.imports named to src/main/resources/META-INF/spring and add my autoconfiguration.