测试执行事件

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。

EventPublishingTestExecutionListener默认注册;但是,只有当ApplicationContext已经被加载时,它才会发布事件。这可以防止ApplicationContext被不必要地或过早地加载。

因此,BeforeTestClassEvent将在另一个TestExecutionListener加载ApplicationContext后才被发布。例如,使用默认的一组注册的TestExecutionListener实现时,对于第一个使用特定测试ApplicationContext的测试类,不会发布BeforeTestClassEvent,但是对于同一测试套件中使用相同测试ApplicationContext的任何后续测试类,将会发布BeforeTestClassEvent,因为当后续测试类运行时,上下文已经被加载(只要上下文没有通过@DirtiesContext或最大大小驱逐策略从ContextCache中删除)。

如果希望确保每个测试类都始终发布BeforeTestClassEvent,则需要注册一个在beforeTestClass回调中加载ApplicationContextTestExecutionListener,并且该TestExecutionListener必须在EventPublishingTestExecutionListener之前注册。

同样,如果使用@DirtiesContext在给定测试类的最后一个测试方法后从上下文缓存中删除ApplicationContext,则不会为该测试类发布AfterTestClassEvent

为了监听测试执行事件,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。