How can I use @WebMvcTest and also add in my own custom filters?

前端 未结 3 1407
遇见更好的自我
遇见更好的自我 2021-01-05 06:55

Spring Boot 1.4 added @WebMvcTest that wire up the parts needed to do test a web slice of my application. This is fantastic, however I also want to ensure my cu

3条回答
  •  没有蜡笔的小新
    2021-01-05 07:03

    @AutoConfigureWebMvc currently import the following auto-configuration classes (see spring.factories in the spring-boot-test-autoconfigure jar):

    # AutoConfigureMockMvc auto-configuration imports
    org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc=\
    org.springframework.boot.test.autoconfigure.web.servlet.MockMvcAutoConfiguration,\
    org.springframework.boot.test.autoconfigure.web.servlet.MockMvcSecurityAutoConfiguration,\
    org.springframework.boot.test.autoconfigure.web.servlet.MockMvcWebClientAutoConfiguration,\
    org.springframework.boot.test.autoconfigure.web.servlet.MockMvcWebDriverAutoConfiguration
    

    Out of this list MockMvcSecurityAutoConfiguration will automatically provide integration with the security filter.

    If you need to add support for additional filters you can write your own MockMvcBuilderCustomizer (see MockMvcSecurityConfiguration.SecurityMockMvcBuilderCustomizer for inspiration).

    You can either use nested @TestConfiguration class to hook your customizer into a specific test, you you could add your own spring.factories and use the AutoConfigureMockMvc key to automatically add it to all tests.

提交回复
热议问题