测试执行事件
Spring Framework 5.2中引入的EventPublishingTestExecutionListener提供了一种实现自定义TestExecutionListener的替代方法。测试的ApplicationContext中的组件可以监听EventPublishingTestExecutionListener发布的以下事件,每个事件对应于TestExecutionListener API中的一个方法。
-
BeforeTestClassEvent -
PrepareTestInstanceEvent -
BeforeTestMethodEvent -
BeforeTestExecutionEvent -
AfterTestExecutionEvent -
AfterTestMethodEvent -
AfterTestClassEvent
这些事件可以被用于各种原因,例如重置模拟bean或跟踪测试执行。消费测试执行事件的一个优势是,相比于实现自定义TestExecutionListener,测试执行事件可以被测试ApplicationContext中注册的任何Spring bean消费,并且这些bean可以直接受益于依赖注入和ApplicationContext的其他功能。相比之下,TestExecutionListener不是ApplicationContext中的一个bean。
|
因此, 如果希望确保每个测试类都始终发布 同样,如果使用 |
为了监听测试执行事件,Spring bean可以选择实现org.springframework.context.ApplicationListener接口。另外,监听器方法可以使用@EventListener进行注释,并配置为监听上面列出的特定事件类型之一(参见基于注释的事件监听器)。由于这种方法的流行,Spring提供了以下专用的@EventListener注释,以简化测试执行事件监听器的注册。这些注释位于org.springframework.test.context.event.annotation包中。
-
@BeforeTestClass -
@PrepareTestInstance -
@BeforeTestMethod -
@BeforeTestExecution -
@AfterTestExecution -
@AfterTestMethod -
@AfterTestClass
异常处理
默认情况下,如果测试执行事件监听器在消费事件时抛出异常,该异常将传播到正在使用的底层测试框架(如JUnit或TestNG)。例如,如果消费BeforeTestMethodEvent导致异常,则由于异常的结果,相应的测试方法将失败。相比之下,如果异步测试执行事件监听器抛出异常,则异常不会传播到底层测试框架。有关异步异常处理的更多详细信息,请参阅@EventListener的类级别javadoc。
异步监听器
如果希望特定的测试执行事件监听器异步处理事件,可以使用Spring的常规@Async支持。有关更多详细信息,请参阅@EventListener的类级别javadoc。