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

TestExecutionListener 配置

Spring 提供了以下 TestExecutionListener 个默认注册的实现,严格按照以下顺序:spring-doc.cadn.net.cn

注册 TestExecutionListener 个实现

您可以使用 @TestExecutionListeners 注解为测试类、其子类和嵌套类显式注册 TestExecutionListener 个实现。请参阅 注解支持@TestExecutionListeners 的 javadoc 以获取详细信息和示例。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实现

通过使用@TestExecutionListeners注册TestExecutionListener个实现的方式,适用于在受限测试场景中使用的自定义监听器。然而,若需在整个测试套件中使用自定义监听器,这种方式会变得繁琐。此问题可通过SpringFactoriesLoader机制支持自动发现默认TestExecutionListener实现的功能来解决。spring-doc.cadn.net.cn

具体来说,spring-test模块在其META-INF/spring.factories属性文件中通过org.springframework.test.context.TestExecutionListener键声明了所有核心默认TestExecutionListener实现。 第三方框架和开发者可以通过各自的META-INF/spring.factories属性文件,以相同方式向默认监听器列表提供自己的TestExecutionListener实现。spring-doc.cadn.net.cn

排序 TestExecutionListener 实现

当TestContext框架通过SpringFactoriesLoader机制发现默认的TestExecutionListener实现时,实例化的监听器会使用Spring的AnnotationAwareOrderComparator进行排序,该机制遵循Spring的Ordered接口和用于排序的@Order注解。AbstractTestExecutionListener及Spring提供的所有默认TestExecutionListener实现均通过适当的数值实现Ordered。因此第三方框架和开发者应确保其默认TestExecutionListener实现通过实现Ordered或声明@Order来按正确顺序注册。有关各核心监听器赋值细节,请参阅核心默认TestExecutionListener实现的getOrder()方法javadoc。spring-doc.cadn.net.cn

合并 TestExecutionListener 个实现方案

如果通过@TestExecutionListeners注册了自定义TestExecutionListener,则不会注册默认监听器。在大多数常见测试场景中,这实际上会强制开发人员除了声明任何自定义监听器外,还必须手动声明所有默认监听器。以下配置示例演示了这种风格: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版本中新增的。不仅如此,像Spring Boot和Spring Security这样的第三方框架,也通过前述的自动发现机制注册了它们自己的默认TestExecutionListener实现。spring-doc.cadn.net.cn

为了避免需要关注并重新声明所有默认监听器,您可以将mergeMode@TestExecutionListeners属性设置为MergeMode.MERGE_WITH_DEFAULTSMERGE_WITH_DEFAULTS表示应将本地声明的监听器与默认监听器合并。该合并算法确保从列表中移除重复项, 并根据TestExecutionListener实现类的排序》中描述的AnnotationAwareOrderComparator语义对合并后的监听器集合进行排序。 若监听器实现了Ordered或使用@Order注解,则其可影响与默认监听器合并时的位置。 否则,在合并时本地声明的监听器将被追加到默认监听器列表的末尾。spring-doc.cadn.net.cn

例如,若前例中的 MyCustomTestExecutionListener 类将其 order 值(例如 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...
}