对于最新稳定版本,请使用Spring Framework 7.0.1spring-doc.cadn.net.cn

TestExecutionListener配置

Spring带来以下效果TestExecutionListener已注册的实现 默认情况下,顺序如下:spring-doc.cadn.net.cn

注册TestExecutionListener实现

你可以注册TestExecutionListener对于测试类、其子类及其嵌套类,通过使用@TestExecutionListeners注解。 参见注释支持和 javadoc@TestExecutionListeners提供详细信息和示例。spring-doc.cadn.net.cn

切换到默认模式TestExecutionListener实现

如果你扩展了一个注释为@TestExecutionListeners你需要切换到默认的监听器集合,你可以用 以后。spring-doc.cadn.net.cn

// Switch to default listeners
@TestExecutionListeners(
	listeners = {},
	inheritListeners = false,
	mergeMode = MERGE_WITH_DEFAULTS)
class MyTest extends BaseTest {
	// class body...
}
// Switch to default listeners
@TestExecutionListeners(
	listeners = [],
	inheritListeners = false,
	mergeMode = MERGE_WITH_DEFAULTS)
class MyTest : BaseTest {
	// class body...
}

自动发现违约TestExecutionListener实现

注册TestExecutionListener通过使用@TestExecutionListeners是 适合在有限测试场景中使用的自定义监听器。然而,如果需要在整个测试套件中使用自定义监听器,可能会变得变得繁琐。 这 问题通过支持自动发现默认值得到解决TestExecutionListener通过SpringFactoriesLoader机制。spring-doc.cadn.net.cn

具体来说,是春季测试模块声明所有核心默认TestExecutionListenerorg.springframework.test.context.TestExecutionListener输入 其META-INF/spring.factories属性文件。第三方框架和开发者可以贡献自己的TestExecutionListener默认列表的实现监听者通过自身META-INF/spring.factories性能 文件。spring-doc.cadn.net.cn

订购TestExecutionListener实现

当TestContext框架发现默认时TestExecutionListener实现 通过上述 SpringFactoriesLoader机制中,实例化的监听者通过以下方式排序Spring的AnnotationAwareOrderComparator,向斯普林致敬命令接口和@Order下单注释。AbstractTestExecutionListener以及所有默认情况TestExecutionListenerSpring 提供的实现命令跟 适当的价值观。因此,第三方框架和开发者应确保他们的默认值TestExecutionListener实现按正确顺序注册通过实现命令或者宣告@Order. 请参见 javadoc 中的getOrder()核心默认方法的方法TestExecutionListener关于具体分配给每个核心监听器的值的详细信息实现。spring-doc.cadn.net.cn

合并TestExecutionListener实现

如果是习俗TestExecutionListener注册方式为@TestExecutionListeners这 默认监听器未被注册。在大多数常见测试场景中,这实际上迫使开发者手动声明所有默认监听器以及任何自定义监听器 听众。 以下列表展示了这种配置风格:spring-doc.cadn.net.cn

@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通过上述自动发现机制实现。spring-doc.cadn.net.cn

为了避免需要重新声明所有默认监听器,你可以设置合并模式属性@TestExecutionListenersMergeMode.MERGE_WITH_DEFAULTS.MERGE_WITH_DEFAULTS表示本地声明的监听器应与默认监听器合并。合并算法确保重复的列表被移除,并根据语义排序合并后的监听器集合 之AnnotationAwareOrderComparator,如描述订购TestExecutionListener实现. 如果监听者实现命令或注释为@Order,它可以影响与默认听器合并的位置。否则,本地声明的监听者在合并时会被附加到默认监听者的列表中。spring-doc.cadn.net.cn

例如,如果MyCustomTestExecutionListener前述例子中的类配置其次序值(例如,500)小于 的阶数ServletTestExecutionListener(恰好是1000),MyCustomTestExecutionListener然后可以自动与默认值列表合并ServletTestExecutionListener,且前述例子可以被以下形式替换:spring-doc.cadn.net.cn

@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...
}