|
对于最新的稳定版本,请使用 Spring Framework 6.2.10! |
TestExecutionListener配置
Spring 提供了以下内容TestExecutionListener已注册的实现
默认情况下,完全按以下顺序:
-
ServletTestExecutionListener:配置 Servlet API 模拟WebApplicationContext. -
DirtiesContextBeforeModesTestExecutionListener:处理@DirtiesContext“之前”模式的注释。 -
ApplicationEventsTestExecutionListener:提供支持ApplicationEvents. -
DependencyInjectionTestExecutionListener:为测试提供依赖注入 实例。 -
MicrometerObservationRegistryTestExecutionListener:提供支持 千分尺的ObservationRegistry. -
DirtiesContextTestExecutionListener:处理@DirtiesContext注释 “之后”模式。 -
TransactionalTestExecutionListener:提供事务性测试执行 默认回滚语义。 -
SqlScriptsTestExecutionListener:运行使用@Sql注解。 -
EventPublishingTestExecutionListener:将测试执行事件发布到测试的ApplicationContext(请参阅测试执行事件)。
注册TestExecutionListener实现
您可以注册TestExecutionListener实现,其
子类及其嵌套类,使用@TestExecutionListeners注解。请参阅注释支持和 javadoc@TestExecutionListeners了解详细信息和示例。
|
切换到默认值
TestExecutionListener实现如果扩展一个用
|
自动发现违约值TestExecutionListener实现
注册TestExecutionListener使用@TestExecutionListeners是
适用于在有限测试场景中使用的自定义监听器。但是,它可以
如果需要在整个测试套件中使用自定义侦听器,则变得很麻烦。这
通过支持自动发现默认值来解决问题TestExecutionListener通过SpringFactoriesLoader机制。
例如,spring-test模块声明所有核心默认值TestExecutionListener在org.springframework.test.context.TestExecutionListener键入
其META-INF/spring.factories属性文件.第三方框架和开发人员可以贡献自己的TestExecutionListener实现到同一
通过自己的方式spring.factories文件。
订购TestExecutionListener实现
当 TestContext 框架发现默认TestExecutionListener实现
通过上述
SpringFactoriesLoader机制,实例化的侦听器通过使用
Spring的AnnotationAwareOrderComparator,这尊重了 Spring 的Orderedinterface 和@Order排序的注释。AbstractTestExecutionListener和所有默认值TestExecutionListenerSpring implementes提供的实现Ordered跟
适当的值。因此,第三方框架和开发人员应确保
他们的默认值TestExecutionListener实现按正确的顺序注册
通过实现Ordered或声明@Order.请参阅 javadoc 中的getOrder()核心默认值的方法TestExecutionListener实现,了解
值分配给每个核心侦听器。
合并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表示本地声明的侦听器应与
默认监听器。合并算法确保从
列表,并且生成的合并侦听器集根据语义进行排序
之AnnotationAwareOrderComparator,如订购TestExecutionListener实现.
如果侦听器实现Ordered或用@Order,它可以影响
它与默认值合并的位置。否则,本地声明的侦听器
合并时附加到默认侦听器列表中。
例如,如果MyCustomTestExecutionListenerclass 在前面的例子中
配置其order值(例如,500) 小于ServletTestExecutionListener(恰好是1000)、MyCustomTestExecutionListener然后可以自动与
默认值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...
}