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

前端 未结 3 1388
遇见更好的自我
遇见更好的自我 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:08

    In addition to Spring Boot options @Phil Webb pointed out, you can use plain Spting Framework features and do something like this:

    @Autowired
    private WebApplicationContext context;
    
    @Autowired
    private FilterChainProxy springSecurityFilter;
    
    @Before
    public void setup() {
        mockMvc = MockMvcBuilders
                .webAppContextSetup(context)
                .addFilters(springSecurityFilter)
                .apply(SecurityMockMvcConfigurers.springSecurity())
                .build();
    }
    

提交回复
热议问题