对于最新的稳定版本,请使用 Spring Framework 6.2.0! |
TestExecutionListener
配置
Spring 提供了以下TestExecutionListener
已注册的 implementations
默认情况下,完全按以下顺序:
-
ServletTestExecutionListener
:为WebApplicationContext
. -
DirtiesContextBeforeModesTestExecutionListener
:处理@DirtiesContext
“before” 模式的注释。 -
ApplicationEventsTestExecutionListener
:提供支持ApplicationEvents
. -
DependencyInjectionTestExecutionListener
:为测试提供依赖项注入 实例。 -
MicrometerObservationRegistryTestExecutionListener
:提供支持 千分尺ObservationRegistry
. -
DirtiesContextTestExecutionListener
:处理@DirtiesContext
的注释 “after” 模式。 -
TransactionalTestExecutionListener
:提供事务测试执行 default 回滚语义。 -
SqlScriptsTestExecutionListener
:运行使用@Sql
注解。 -
EventPublishingTestExecutionListener
:将测试执行事件发布到测试的ApplicationContext
(请参阅 测试执行事件)。
注册TestExecutionListener
实现
您可以注册TestExecutionListener
implementation 的
子类及其嵌套类。@TestExecutionListeners
注解。请参阅注释支持和 javadoc 以获取@TestExecutionListeners
了解详细信息和示例。
切换到默认
TestExecutionListener 实现如果您扩展了一个注解有
|
自动发现默认TestExecutionListener
实现
注册TestExecutionListener
使用@TestExecutionListeners
是
适用于在有限测试场景中使用的自定义监听器。但是,它可以
如果需要在整个测试套件中使用自定义侦听器,则会变得很麻烦。这
此问题已通过支持自动发现默认得到解决TestExecutionListener
通过SpringFactoriesLoader
机制。
例如,spring-test
module declars all core 默认TestExecutionListener
implementation 在org.springframework.test.context.TestExecutionListener
键入
其META-INF/spring.factories
属性文件.第三方框架和开发人员可以贡献自己的框架TestExecutionListener
implementations 添加到同一
方式spring.factories
文件。
订购TestExecutionListener
实现
当 TestContext 框架发现默认TestExecutionListener
实现
通过上述
SpringFactoriesLoader
机制中,实例化的侦听器通过使用
Spring的AnnotationAwareOrderComparator
,它尊重 Spring 的Ordered
interface 和@Order
注解进行排序。AbstractTestExecutionListener
并且全部默认TestExecutionListener
Spring implements 提供的实现Ordered
跟
适当的值。因此,第三方框架和开发人员应确保
他们的默认TestExecutionListener
实现按正确的顺序注册
通过实施Ordered
或声明@Order
.请参阅 javadoc 以获取getOrder()
核心 default 的方法TestExecutionListener
implementations (实现) 详细了解
值将分配给每个核心侦听器。
合并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
使用上述 Automatic Discovery mechanism 实现。
为避免必须了解并重新声明所有默认侦听器,您可以设置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...
}