此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.1.10Spring中文文档

此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.1.10Spring中文文档

Spring 提供了以下已注册的实现 默认情况下,完全按以下顺序排列:TestExecutionListenerSpring中文文档

注册实现TestExecutionListener

您可以显式注册测试类的实现,其 子类及其嵌套类,使用注释。有关详细信息和示例,请参阅注释支持javadoc for @TestExecutionListenersTestExecutionListener@TestExecutionListenersSpring中文文档

切换到默认实现TestExecutionListener

如果扩展了注释的类,并且需要 切换到使用默认的侦听器集,您可以使用 以后。@TestExecutionListenersSpring中文文档

// 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

如果扩展了注释的类,并且需要 切换到使用默认的侦听器集,您可以使用 以后。@TestExecutionListenersSpring中文文档

// 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@TestExecutionListenersTestExecutionListenerSpringFactoriesLoaderSpring中文文档

具体来说,该模块在 其属性文件。第三方框架和开发人员 可以将自己的实现贡献到默认列表中 侦听器以同样的方式通过他们自己的属性 文件。spring-testTestExecutionListenerorg.springframework.test.context.TestExecutionListenerMETA-INF/spring.factoriesTestExecutionListenerMETA-INF/spring.factoriesSpring中文文档

对实现进行排序TestExecutionListener

当 TestContext 框架发现默认实现时 通过上述机制,实例化的侦听器按 Spring's ,它尊重 Spring 的界面和排序注释。 以及 Spring 提供的所有默认实现 适当的值。因此,第三方框架和开发人员应确保 它们的默认实现按正确的顺序注册 通过实现或声明 .有关核心默认实现的方法,请参阅 javadoc,了解详细信息 值分配给每个核心侦听器。TestExecutionListenerSpringFactoriesLoaderAnnotationAwareOrderComparatorOrdered@OrderAbstractTestExecutionListenerTestExecutionListenerOrderedTestExecutionListenerOrdered@OrdergetOrder()TestExecutionListenerSpring中文文档

合并实现TestExecutionListener

如果自定义是通过 注册的, 未注册默认侦听器。在最常见的测试场景中,这有效地 强制开发人员手动声明除任何自定义之外的所有默认侦听器 听众。以下列表演示了这种配置方式:TestExecutionListener@TestExecutionListenersSpring中文文档

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

这种方法的挑战在于,它要求开发人员确切地知道 默认情况下会注册哪些侦听器。此外,默认侦听器集可以 从一个版本到另一个版本的变化 - 例如,是 在 Spring Framework 4.1 中引入,并在 Spring Framework 4.2 中引入。此外,像 Spring 这样的第三方框架 Boot 和 Spring Security 使用上述自动发现机制注册自己的默认实现。SqlScriptsTestExecutionListenerDirtiesContextBeforeModesTestExecutionListenerTestExecutionListenerSpring中文文档

为了避免必须知道并重新声明所有默认侦听器,您可以将 的属性设置为 。 指示本地声明的侦听器应与 默认侦听器。合并算法可确保从 列表,并且根据语义对生成的合并侦听器集进行排序 的,如TestExecutionListener 实现进行排序中所述。 如果侦听器实现或被注释为 ,它可以影响 它与默认值合并的位置。否则,本地声明的侦听器 合并时将追加到默认侦听器列表中。mergeMode@TestExecutionListenersMergeMode.MERGE_WITH_DEFAULTSMERGE_WITH_DEFAULTSAnnotationAwareOrderComparatorOrdered@OrderSpring中文文档

例如,如果上一个示例中的类 将其值(例如,)配置为小于 (恰好是 ) 的顺序,然后可以自动与 默认值在 前面,前面的例子可以 替换为以下内容:MyCustomTestExecutionListenerorder500ServletTestExecutionListener1000MyCustomTestExecutionListenerServletTestExecutionListenerSpring中文文档

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