|
对于最新稳定版本,请使用Spring Framework 7.0.1! |
TestExecutionListener配置
Spring带来以下效果TestExecutionListener已注册的实现
默认情况下,顺序如下:
-
ServletTestExecutionListener: 配置 Servlet API mocks for aWebApplicationContext. -
DirtiesContextBeforeModesTestExecutionListener:处理@DirtiesContext“之前”模式的注释。 -
ApplicationEventsTestExecutionListener:提供支持应用事件. -
DependencyInjectionTestExecutionListener:为检验提供依赖注入 实例。 -
MicrometerObservationRegistryTestExecutionListener:提供支持 微米ObservationRegistry. -
DirtiesContextTestExecutionListener:处理@DirtiesContext注释 “之后”模式。 -
TransactionalTestExecutionListener:提供事务测试执行,支持默认回滚语义。 -
SqlScriptsTestExecutionListener运行通过使用@Sql注解。 -
EventPublishingTestExecutionListener:将测试执行事件发布到测试的应用上下文(参见测试执行事件)
注册TestExecutionListener实现
你可以注册TestExecutionListener对于测试类、其子类及其嵌套类,通过使用@TestExecutionListeners注解。 参见注释支持和 javadoc@TestExecutionListeners提供详细信息和示例。
|
切换到默认模式
TestExecutionListener实现如果你扩展了一个注释为
|
自动发现违约TestExecutionListener实现
注册TestExecutionListener通过使用@TestExecutionListeners是 适合在有限测试场景中使用的自定义监听器。然而,如果需要在整个测试套件中使用自定义监听器,可能会变得变得繁琐。 这 问题通过支持自动发现默认值得到解决TestExecutionListener通过SpringFactoriesLoader机制。
具体来说,是春季测试模块声明所有核心默认TestExecutionListener在org.springframework.test.context.TestExecutionListener输入 其META-INF/spring.factories属性文件。第三方框架和开发者可以贡献自己的TestExecutionListener默认列表的实现监听者通过自身META-INF/spring.factories性能 文件。
订购TestExecutionListener实现
当TestContext框架发现默认时TestExecutionListener实现 通过上述
SpringFactoriesLoader机制中,实例化的监听者通过以下方式排序Spring的AnnotationAwareOrderComparator,向斯普林致敬命令接口和@Order下单注释。AbstractTestExecutionListener以及所有默认情况TestExecutionListenerSpring 提供的实现命令跟 适当的价值观。因此,第三方框架和开发者应确保他们的默认值TestExecutionListener实现按正确顺序注册通过实现命令或者宣告@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通过上述自动发现机制实现。
为了避免需要重新声明所有默认监听器,你可以设置合并模式属性@TestExecutionListeners自MergeMode.MERGE_WITH_DEFAULTS.MERGE_WITH_DEFAULTS表示本地声明的监听器应与默认监听器合并。合并算法确保重复的列表被移除,并根据语义排序合并后的监听器集合 之AnnotationAwareOrderComparator,如描述订购TestExecutionListener实现. 如果监听者实现命令或注释为@Order,它可以影响与默认听器合并的位置。否则,本地声明的监听者在合并时会被附加到默认监听者的列表中。
例如,如果MyCustomTestExecutionListener前述例子中的类配置其次序值(例如,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...
}