此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Framework 6.2.10! |
TestExecutionListener
配置
Spring 提供了以下内容TestExecutionListener
已注册的实现
默认情况下,完全按以下顺序:
-
ServletTestExecutionListener
:配置 Servlet API 模拟WebApplicationContext
. -
DirtiesContextBeforeModesTestExecutionListener
:处理@DirtiesContext
“之前”模式的注释。 -
ApplicationEventsTestExecutionListener
:提供支持ApplicationEvents
. -
BeanOverrideTestExecutionListener
:提供对测试中 Bean 覆盖的支持。 -
DependencyInjectionTestExecutionListener
:为测试提供依赖注入 实例。 -
MicrometerObservationRegistryTestExecutionListener
:提供支持 千分尺的ObservationRegistry
. -
DirtiesContextTestExecutionListener
:处理@DirtiesContext
注释 “之后”模式。 -
CommonCachesTestExecutionListener
:清除测试中ApplicationContext
如有必要。 -
TransactionalTestExecutionListener
:提供事务性测试执行 默认回滚语义。 -
SqlScriptsTestExecutionListener
:运行使用@Sql
注解。 -
EventPublishingTestExecutionListener
:将测试执行事件发布到测试的ApplicationContext
(请参阅测试执行事件)。 -
MockitoResetTestExecutionListener
:重置配置的模拟@MockitoBean
或@MockitoSpyBean
.
注册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 的Ordered
interface 和@Order
排序的注释。AbstractTestExecutionListener
和所有默认值TestExecutionListener
Spring 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 中引入的。此外,像 SpringBoot 和 Spring Security 这样的第三方框架注册了自己的默认值TestExecutionListener
使用上述自动发现机制实现。
为了避免必须了解并重新声明所有默认监听器,您可以将mergeMode
属性@TestExecutionListeners
自MergeMode.MERGE_WITH_DEFAULTS
.MERGE_WITH_DEFAULTS
表示本地声明的侦听器应与
默认监听器。合并算法确保从
列表,并且生成的合并侦听器集根据语义进行排序
之AnnotationAwareOrderComparator
,如订购TestExecutionListener
实现. 如果侦听器实现Ordered
或用@Order
,它会影响它与默认值合并的位置。否则,本地声明的侦听器在合并时将附加到默认侦听器列表中。
例如,如果MyCustomTestExecutionListener
class 在前面的示例中配置其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...
}