如何使用@WebMvcTest并添加自己的自定义过滤器? [英] How can I use @WebMvcTest and also add in my own custom filters?

查看:431
本文介绍了如何使用@WebMvcTest并添加自己的自定义过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring Boot 1.4添加了@WebMvcTest,用于连接测试我的应用程序的Web切片所需的部分.这太棒了,但是我还想确保已连接我的自定义过滤器和安全代码,以便我也可以验证它是否正常工作.

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 custom filters and security code is wired up so I can validate that is working correctly too.

使用@WebMvcTest时如何添加自定义过滤器?

How can I add in custom filters when using @WebMvcTest?

推荐答案

@AutoConfigureWebMvc当前导入以下自动配置类(请参见spring-boot-test-autoconfigure jar中的spring.factories):

@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

在此列表中,MockMvcSecurityAutoConfiguration将自动提供与安全过滤器的集成.

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

如果您需要增加对其他过滤器的支持,则可以编写自己的MockMvcBuilderCustomizer(有关灵感,请参见MockMvcSecurityConfiguration.SecurityMockMvcBuilderCustomizer).

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

您可以使用嵌套的@TestConfiguration类将定制程序挂接到特定测试中,也可以添加自己的spring.factories并使用AutoConfigureMockMvc键将其自动添加到所有测试中.

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.

这篇关于如何使用@WebMvcTest并添加自己的自定义过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆