TestExecutionListener配置
Spring 提供了以下TestExecutionListener已注册的 implementations
默认情况下,完全按以下顺序:
-
ServletTestExecutionListener:为WebApplicationContext. -
DirtiesContextBeforeModesTestExecutionListener:处理@DirtiesContext“before” 模式的注释。 -
ApplicationEventsTestExecutionListener:提供支持ApplicationEvents. -
BeanOverrideTestExecutionListener:为测试中的 Bean 覆盖提供支持。 -
DependencyInjectionTestExecutionListener:为测试提供依赖项注入 实例。 -
MicrometerObservationRegistryTestExecutionListener:提供支持 千分尺ObservationRegistry. -
DirtiesContextTestExecutionListener:处理@DirtiesContext的注释 “after” 模式。 -
CommonCachesTestExecutionListener:清除测试的ApplicationContext如有必要。 -
TransactionalTestExecutionListener:提供事务测试执行 default 回滚语义。 -
SqlScriptsTestExecutionListener:运行使用@Sql注解。 -
EventPublishingTestExecutionListener:将测试执行事件发布到测试的ApplicationContext(请参阅 测试执行事件)。 -
MockitoResetTestExecutionListener:按照@MockitoBean或@MockitoSpyBean.
注册TestExecutionListener实现
您可以注册TestExecutionListenerimplementation 的
子类及其嵌套类。@TestExecutionListeners注解。请参阅注释支持和 javadoc 以获取@TestExecutionListeners了解详细信息和示例。
|
切换到默认
TestExecutionListener实现如果您扩展了一个注解有
|
自动发现默认TestExecutionListener实现
注册TestExecutionListener使用@TestExecutionListeners是
适用于在有限测试场景中使用的自定义监听器。但是,它可以
如果需要在整个测试套件中使用自定义侦听器,则会变得很麻烦。这
此问题已通过支持自动发现默认得到解决TestExecutionListener通过SpringFactoriesLoader机制。
例如,spring-testmodule declars all core 默认TestExecutionListenerimplementation 在org.springframework.test.context.TestExecutionListener键入
其META-INF/spring.factories属性文件.第三方框架和开发人员可以贡献自己的框架TestExecutionListenerimplementations 添加到同一
方式spring.factories文件。
订购TestExecutionListener实现
当 TestContext 框架发现默认TestExecutionListener实现
通过上述
SpringFactoriesLoader机制中,实例化的侦听器通过使用
Spring的AnnotationAwareOrderComparator,它尊重 Spring 的Orderedinterface 和@Order注解进行排序。AbstractTestExecutionListener并且全部默认TestExecutionListenerSpring implements 提供的实现Ordered跟
适当的值。因此,第三方框架和开发人员应确保
他们的默认TestExecutionListener实现按正确的顺序注册
通过实施Ordered或声明@Order.请参阅 javadoc 以获取getOrder()核心 default 的方法TestExecutionListenerimplementations (实现) 详细了解
值将分配给每个核心侦听器。
合并TestExecutionListener实现
如果自定义TestExecutionListener通过@TestExecutionListeners这
默认侦听器未注册。在最常见的测试场景中,这有效地
强制开发人员手动声明所有默认侦听器以及任何自定义
听众。下面的清单演示了这种配置样式:
-
Java
-
Kotlin
@ContextConfiguration
@TestExecutionListeners({
MyCustomTestExecutionListener.class,
ServletTestExecutionListener.class,
DirtiesContextBeforeModesTestExecutionListener.class,
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
SqlScriptsTestExecutionListener.class
})
class MyTest {
// class body...
}
@ContextConfiguration
@TestExecutionListeners(
MyCustomTestExecutionListener::class,
ServletTestExecutionListener::class,
DirtiesContextBeforeModesTestExecutionListener::class,
DependencyInjectionTestExecutionListener::class,
DirtiesContextTestExecutionListener::class,
TransactionalTestExecutionListener::class,
SqlScriptsTestExecutionListener::class
)
class MyTest {
// class body...
}
这种方法的挑战在于它要求开发人员确切地知道
默认注册哪些侦听器。此外,默认侦听器集可以
从一个版本更改为另一个版本 — 例如,SqlScriptsTestExecutionListener是
在 Spring Framework 4.1 中引入,以及DirtiesContextBeforeModesTestExecutionListener是在 Spring Framework 4.2 中引入的。此外,像 Spring 这样的第三方框架
Boot 和 Spring Security 注册自己的默认值TestExecutionListener使用上述自动发现机制实现。
为避免必须了解并重新声明所有默认侦听器,您可以设置mergeMode属性@TestExecutionListeners自MergeMode.MERGE_WITH_DEFAULTS.MERGE_WITH_DEFAULTS指示本地声明的侦听器应与
default 侦听器。合并算法可确保从
list 中,并且生成的合并侦听器集根据语义进行排序
之AnnotationAwareOrderComparator,如订购TestExecutionListener实现.
如果侦听器实现了Ordered或带有@Order,它会影响
位置,它与默认值合并。否则,本地声明的侦听器
在合并时附加到默认侦听器列表中。
例如,如果MyCustomTestExecutionListener类
配置其order值(例如500) 小于ServletTestExecutionListener(恰好是1000)、MyCustomTestExecutionListener然后,可以自动与
defaults 位于ServletTestExecutionListener,而前面的示例可以
替换为以下内容:
-
Java
-
Kotlin
@ContextConfiguration
@TestExecutionListeners(
listeners = MyCustomTestExecutionListener.class,
mergeMode = MERGE_WITH_DEFAULTS
)
class MyTest {
// class body...
}
@ContextConfiguration
@TestExecutionListeners(
listeners = [MyCustomTestExecutionListener::class],
mergeMode = MERGE_WITH_DEFAULTS
)
class MyTest {
// class body...
}