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

TestExecutionListener配置

Spring 提供了以下TestExecutionListener已注册的 implementations 默认情况下,完全按以下顺序:spring-doc.cadn.net.cn

注册TestExecutionListener实现

您可以注册TestExecutionListenerimplementation 的 子类及其嵌套类。@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...
}

自动发现默认TestExecutionListener实现

注册TestExecutionListener使用@TestExecutionListeners是 适用于在有限测试场景中使用的自定义监听器。但是,它可以 如果需要在整个测试套件中使用自定义侦听器,则会变得很麻烦。这 此问题已通过支持自动发现默认得到解决TestExecutionListener通过SpringFactoriesLoader机制。spring-doc.cadn.net.cn

例如,spring-testmodule declars all core 默认TestExecutionListenerimplementation 在org.springframework.test.context.TestExecutionListener键入 其META-INF/spring.factories属性文件.第三方框架和开发人员可以贡献自己的框架TestExecutionListenerimplementations 添加到同一 方式spring.factories文件。spring-doc.cadn.net.cn

订购TestExecutionListener实现

当 TestContext 框架发现默认TestExecutionListener实现 通过上述 SpringFactoriesLoader机制中,实例化的侦听器通过使用 Spring的AnnotationAwareOrderComparator,它尊重 Spring 的Orderedinterface 和@Order注解进行排序。AbstractTestExecutionListener并且全部默认TestExecutionListenerSpring implements 提供的实现Ordered跟 适当的值。因此,第三方框架和开发人员应确保 他们的默认TestExecutionListener实现按正确的顺序注册 通过实施Ordered或声明@Order.请参阅 javadoc 以获取getOrder()核心 default 的方法TestExecutionListenerimplementations (实现) 详细了解 值将分配给每个核心侦听器。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...
}

这种方法的挑战在于它要求开发人员确切地知道 默认注册哪些侦听器。此外,默认侦听器集可以 从一个版本更改为另一个版本 — 例如,SqlScriptsTestExecutionListener是 在 Spring Framework 4.1 中引入,以及DirtiesContextBeforeModesTestExecutionListener是在 Spring Framework 4.2 中引入的。此外,像 Spring 这样的第三方框架 Boot 和 Spring Security 注册自己的默认值TestExecutionListener使用上述 Automatic Discovery mechanism 实现。spring-doc.cadn.net.cn

为避免必须了解并重新声明所有默认侦听器,您可以设置mergeMode属性@TestExecutionListenersMergeMode.MERGE_WITH_DEFAULTS.MERGE_WITH_DEFAULTS指示本地声明的侦听器应与 default 侦听器。合并算法可确保从 list 中,并且生成的合并侦听器集根据语义进行排序 之AnnotationAwareOrderComparator,如订购TestExecutionListener实现. 如果侦听器实现了Ordered或带有@Order,它会影响 位置,它与默认值合并。否则,本地声明的侦听器 在合并时附加到默认侦听器列表中。spring-doc.cadn.net.cn

例如,如果MyCustomTestExecutionListener类 配置其order值(例如500) 小于ServletTestExecutionListener(恰好是1000)、MyCustomTestExecutionListener然后,可以自动与 defaults 位于ServletTestExecutionListener,而前面的示例可以 替换为以下内容:spring-doc.cadn.net.cn

@ContextConfiguration
@TestExecutionListeners(
	listeners = MyCustomTestExecutionListener.class,
	mergeMode = MERGE_WITH_DEFAULTS
)
class MyTest {
	// class body...
}

APP信息