Spring Boot 2.1 - @WebMvcTest without Spring Security Auto-Configuration

后端 未结 2 2037
渐次进展
渐次进展 2021-02-19 00:27

Before migrating to Spring Boot 2.1, we had a couple of controller tests in our services utilizing @WebMvcTest in combination with @AutoConfigureMockMvc

相关标签:
2条回答
  • 2021-02-19 00:52

    Yes, rather than working around the fact the flag is deprecated, you should embrace the fact that this is going in that direction going forward.

    As of Spring Boot 2.1, if you have Spring Security, your tests will be secured using your custom configuration. What is the actual problem with that?

    If you don't want to authenticate for certain tests, just use Spring Security's test infrastructure and add @WithMockUser.

    0 讨论(0)
  • 2021-02-19 00:54

    Encountered the same scenario and what helped was using the below annotations instead of @WebMvcTest. In this case, @WithMockUser did not help.

    @WebAppConfiguration
    @Import({MockMvcAutoConfiguration.class})
    @EnableConfigurationProperties({ResourceProperties.class, WebMvcProperties.class})
    

    Classes that existed in controllers / value of @WebMvcTest goes into value of @Import annotation.

    Source: https://github.com/spring-projects/spring-boot/issues/14227#issuecomment-688824627

    0 讨论(0)
提交回复
热议问题