Configuration Configuration with Context Customizers
ContextCustomizer
负责在 Bean 定义已加载到上下文中,但在上下文已刷新之前,自定义提供的 ConfigurableApplicationContext
。
A ContextCustomizer
is responsible for customizing the supplied
ConfigurableApplicationContext
after bean definitions have been loaded into the context
but before the context has been refreshed.
ContextCustomizerFactory
负责创建 ContextCustomizer
,它基于一些自定义逻辑来确定 ContextCustomizer
是否对于给定的测试类是必要的——例如,基于某个注释的存在。在 ContextLoaders
处理了测试类的上下文配置属性后,但在创建 MergedContextConfiguration
之前,便会调用工厂。
A ContextCustomizerFactory
is responsible for creating a ContextCustomizer
, based on
some custom logic which determines if the ContextCustomizer
is necessary for a given
test class — for example, based on the presence of a certain annotation. Factories are
invoked after ContextLoaders
have processed context configuration attributes for a test
class but before the MergedContextConfiguration
is created.
例如,Spring Framework 提供以下 ContextCustomizerFactory
,默认情况下已注册该实现:
For example, Spring Framework provides the following ContextCustomizerFactory
implementation which is registered by default:
MockServerContainerContextCustomizerFactory
:: Creates a
MockServerContainerContextCustomizer
if WebSocket support is present in the classpath
and the test class or one of its enclosing classes is annotated or meta-annotated with
@WebAppConfiguration
. MockServerContainerContextCustomizer
instantiates a new
MockServerContainer
and stores it in the ServletContext
under the attribute named
jakarta.websocket.server.ServerContainer
.
Registering ContextCustomizerFactory
Implementations
你可以显式地为测试类及其子类和嵌套类注册 ContextCustomizerFactory
实现,方法是使用 @ContextCustomizerFactories
注释。请参见 annotation support 和 @ContextCustomizerFactories
的 javadoc 以获取详细信息和示例。
You can register ContextCustomizerFactory
implementations explicitly for a test class, its
subclasses, and its nested classes by using the @ContextCustomizerFactories
annotation. See
annotation support
and the javadoc for
@ContextCustomizerFactories
for details and examples.
Automatic Discovery of Default ContextCustomizerFactory
Implementations
使用 @ContextCustomizerFactories
注册 ContextCustomizerFactory
实现适用于在有限的测试场景中使用的自定义工厂。但是,如果在整个测试套件中需要使用自定义工厂,则可能会变得繁琐。可以通过 SpringFactoriesLoader
机制支持自动发现默认 ContextCustomizerFactory
实现来解决此问题。
Registering ContextCustomizerFactory
implementations by using @ContextCustomizerFactories
is
suitable for custom factories that are used in limited testing scenarios. However, it can
become cumbersome if a custom factory needs to be used across an entire test suite. This
issue is addressed through support for automatic discovery of default
ContextCustomizerFactory
implementations through the SpringFactoriesLoader
mechanism.
例如,组成 Spring Framework 和 Spring Boot 中的测试支持的模块在其 META-INF/spring.factories
属性文件中的 org.springframework.test.context.ContextCustomizerFactory
键下声明了所有核心的默认 ContextCustomizerFactory
实现。spring-test
模块的 spring.factories
文件可以查看 此处。第三方框架和开发人员可以通过自己的 spring.factories
文件以相同的方式向默认工厂列表中添加自己的 ContextCustomizerFactory
实现。
For example, the modules that make up the testing support in Spring Framework and Spring
Boot declare all core default ContextCustomizerFactory
implementations under the
org.springframework.test.context.ContextCustomizerFactory
key in their
META-INF/spring.factories
properties files. The spring.factories
file for the
spring-test
module can be viewed
here.
Third-party frameworks and developers can contribute their own ContextCustomizerFactory
implementations to the list of default factories in the same manner through their own
spring.factories
files.
Merging ContextCustomizerFactory
Implementations
如果通过 @ContextCustomizerFactories
注册了自定义`ContextCustomizerFactory` ,则它将通过使用上述automatic discovery mechanism 注册的默认工厂进行_merged_ 。
If a custom ContextCustomizerFactory
is registered via @ContextCustomizerFactories
, it
will be merged with the default factories that have been registered using the aforementioned
automatic discovery mechanism.
合并算法确保从列表中删除重复项,并在合并时将本地声明的工厂附加到默认工厂列表中。
The merging algorithm ensures that duplicates are removed from the list and that locally declared factories are appended to the list of default factories when merged.
要替换测试类、其子类和嵌套类的默认工厂,可以将 To replace the default factories for a test class, its subclasses, and its nested
classes, you can set the |