|
对于最新的稳定版本,请使用 Spring Framework 7.0.6! |
TestExecutionListener 配置
Spring 提供了以下 TestExecutionListener 个默认注册的实现,严格按照以下顺序:
-
ServletTestExecutionListener: 为WebApplicationContext配置 Servlet API 模拟对象。 -
DirtiesContextBeforeModesTestExecutionListener: 处理用于“前置”模式的@DirtiesContext注解。 -
ApplicationEventsTestExecutionListener: 提供对ApplicationEvents的支持。 -
DependencyInjectionTestExecutionListener: 为测试实例提供依赖注入。 -
MicrometerObservationRegistryTestExecutionListener: 为 Micrometer的ObservationRegistry提供支持。 -
DirtiesContextTestExecutionListener:处理“之后”模式的@DirtiesContext注解。 -
TransactionalTestExecutionListener: 提供具有默认回滚语义的事务性测试执行。 -
SqlScriptsTestExecutionListener: 通过使用@Sql注解配置运行SQL脚本。 -
EventPublishingTestExecutionListener:将测试执行事件发布到测试的ApplicationContext(请参阅测试执行事件)。
注册 TestExecutionListener 个实现
您可以使用 @TestExecutionListeners 注解为测试类、其子类和嵌套类显式注册 TestExecutionListener 个实现。请参阅 注解支持 和 @TestExecutionListeners 的 javadoc 以获取详细信息和示例。
|
切换到默认
TestExecutionListener 实现如果您扩展了一个用
|
自动发现默认的TestExecutionListener实现
通过使用@TestExecutionListeners注册TestExecutionListener个实现的方式,适用于在受限测试场景中使用的自定义监听器。然而,若需在整个测试套件中使用自定义监听器,这种方式会变得繁琐。此问题可通过SpringFactoriesLoader机制支持自动发现默认TestExecutionListener实现的功能来解决。
具体来说,spring-test模块在其META-INF/spring.factories属性文件中通过org.springframework.test.context.TestExecutionListener键声明了所有核心默认TestExecutionListener实现。
第三方框架和开发者可以通过各自的META-INF/spring.factories属性文件,以相同方式向默认监听器列表提供自己的TestExecutionListener实现。
排序 TestExecutionListener 实现
当TestContext框架通过SpringFactoriesLoader机制发现默认的TestExecutionListener实现时,实例化的监听器会使用Spring的AnnotationAwareOrderComparator进行排序,该机制遵循Spring的Ordered接口和用于排序的@Order注解。AbstractTestExecutionListener及Spring提供的所有默认TestExecutionListener实现均通过适当的数值实现Ordered。因此第三方框架和开发者应确保其默认TestExecutionListener实现通过实现Ordered或声明@Order来按正确顺序注册。有关各核心监听器赋值细节,请参阅核心默认TestExecutionListener实现的getOrder()方法javadoc。
合并 TestExecutionListener 个实现方案
如果通过@TestExecutionListeners注册了自定义TestExecutionListener,则不会注册默认监听器。在大多数常见测试场景中,这实际上会强制开发人员除了声明任何自定义监听器外,还必须手动声明所有默认监听器。以下配置示例演示了这种风格:
-
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表示应将本地声明的监听器与默认监听器合并。该合并算法确保从列表中移除重复项,
并根据《TestExecutionListener实现类的排序》中描述的AnnotationAwareOrderComparator语义对合并后的监听器集合进行排序。
若监听器实现了Ordered或使用@Order注解,则其可影响与默认监听器合并时的位置。
否则,在合并时本地声明的监听器将被追加到默认监听器列表的末尾。
例如,若前例中的 MyCustomTestExecutionListener 类将其 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...
}