Testing
Spring Boot 包括许多测试实用工具和支持类,以及一个专门的 starter,它提供通用的测试依赖项。本节解答有关测试的常见问题。
Spring Boot includes a number of testing utilities and support classes as well as a dedicated starter that provides common test dependencies. This section answers common questions about testing.
Testing With Spring Security
Spring Security 提供支持以特定用户身份运行测试。例如,以下代码段中的测试将使用具有 ADMIN
角色的身份验证用户运行。
Spring Security provides support for running tests as a specific user.
For example, the test in the snippet below will run with an authenticated user that has the ADMIN
role.
Spring Security 提供与 Spring MVC Test 的全面集成,当使用 @WebMvcTest
slice 和 MockMvc
测试控制器时也可以使用该集成。
Spring Security provides comprehensive integration with Spring MVC Test, and this can also be used when testing controllers using the @WebMvcTest
slice and MockMvc
.
有关 Spring Security 测试支持的更多详细信息,请参阅 Spring Security 的 {url-spring-security-docs}/servlet/test/index.html[参考文档]。
For additional details on Spring Security’s testing support, see Spring Security’s {url-spring-security-docs}/servlet/test/index.html[reference documentation].
Structure @Configuration
classes for inclusion in slice tests
Slice 测试通过根据组件类型将其限制在有限的组件集合中来工作,以此限制 Spring Framework 的组件扫描。对于任何未使用组件扫描创建的 bean(例如,使用 @Bean
注解创建的 bean),slice 测试将无法在应用程序上下文中包含/排除它们。考虑以下示例:
Slice tests work by restricting Spring Framework’s component scanning to a limited set of components based on their type.
For any beans that are not created through component scanning, for example, beans that are created using the @Bean
annotation, slice tests will not be able to include/exclude them from the application context.
Consider this example:
对于具有上述 @Configuration
类的应用程序的 @WebMvcTest
,你可能希望在应用程序上下文中拥有 SecurityFilterChain
bean,以便测试控制器端点是否已正确保护。但是,由于 MyConfiguration
不匹配过滤器指定的所有类型,因此不会通过 @WebMvcTest 的组件扫描过滤器将其选中。你可以使用 @Import(MyConfiguration.class)
为测试类添加注释,以明确包含配置。这将加载 MyConfiguration
中的所有 bean,包括在测试 Web 层时不需要的 BasicDataSource
bean。将配置类拆分成两个类,将仅导入安全配置。
For a @WebMvcTest
for an application with the above @Configuration
class, you might expect to have the SecurityFilterChain
bean in the application context so that you can test if your controller endpoints are secured properly.
However, MyConfiguration
is not picked up by @WebMvcTest’s component scanning filter because it doesn’t match any of the types specified by the filter.
You can include the configuration explicitly by annotating the test class with @Import(MyConfiguration.class)
.
This will load all the beans in MyConfiguration
including the BasicDataSource
bean which isn’t required when testing the web tier.
Splitting the configuration class into two will enable importing just the security configuration.
当特定域的 bean 需要包含在 slice 测试中时,拥有一个单一的配置类可能会效率低下。相反,将应用程序的配置构建为具有特定域 bean 的多个细化类,可以仅为特定的 slice 测试导入它们。
Having a single configuration class can be inefficient when beans of a certain domain need to be included in slice tests. Instead, structuring the application’s configuration as multiple granular classes with beans for a specific domain can enable importing them only for specific slice tests.