Application Events

自Spring Framework 5.3.3以来,TestContext框架提供了对在`ApplicationContext`中发布的application events进行记录的支持,以便可以在测试中针对这些事件执行断言。在单一测试执行过程中发布的所有事件都可以通过`ApplicationEvents`API获得,该API允许你将事件作为`java.util.Stream`进行处理。

Since Spring Framework 5.3.3, the TestContext framework provides support for recording application events published in the ApplicationContext so that assertions can be performed against those events within tests. All events published during the execution of a single test are made available via the ApplicationEvents API which allows you to process the events as a java.util.Stream.

要在测试中使用 ApplicationEvents,请执行以下操作。

To use ApplicationEvents in your tests, do the following.

  • 确保您的测试类被通过 @RecordApplicationEvents 注释或者元注释。

  • Ensure that your test class is annotated or meta-annotated with @RecordApplicationEvents.

  • 确保 ApplicationEventsTestExecutionListener 已经注册。但是请注意,ApplicationEventsTestExecutionListener 默认注册,只在通过 @TestExecutionListeners 进行自定义配置时才需要手动注册,而该配置不包括默认侦听器。

  • Ensure that the ApplicationEventsTestExecutionListener is registered. Note, however, that ApplicationEventsTestExecutionListener is registered by default and only needs to be manually registered if you have custom configuration via @TestExecutionListeners that does not include the default listeners.

  • 使用 @Autowired 注释一个类型为 ApplicationEvents 的字段,并在您的测试和生命周期方法(例如,JUnit Jupiter 中的 @BeforeEach@AfterEach 方法)中使用该 ApplicationEvents 实例。

    • 使用 SpringExtension for JUnit Jupiter 时,您可以在测试或生命周期方法中声明类型为 ApplicationEvents 的方法参数,作为测试类中 @Autowired 字段的替代方法。

    • When using the SpringExtension for JUnit Jupiter, you may declare a method parameter of type ApplicationEvents in a test or lifecycle method as an alternative to an @Autowired field in the test class.

  • Annotate a field of type ApplicationEvents with @Autowired and use that instance of ApplicationEvents in your test and lifecycle methods (such as @BeforeEach and @AfterEach methods in JUnit Jupiter).

    • 使用 SpringExtension for JUnit Jupiter 时,您可以在测试或生命周期方法中声明类型为 ApplicationEvents 的方法参数,作为测试类中 @Autowired 字段的替代方法。

    • When using the SpringExtension for JUnit Jupiter, you may declare a method parameter of type ApplicationEvents in a test or lifecycle method as an alternative to an @Autowired field in the test class.

以下测试类使用 JUnit Jupiter 和 AssertJ 的 `SpringExtension`来断言在 Spring 管理的组件中调用方法时发布的应用程序事件的类型:

The following test class uses the SpringExtension for JUnit Jupiter and AssertJ to assert the types of application events published while invoking a method in a Spring-managed component:

  • Java

  • Kotlin

@SpringJUnitConfig(/* ... */)
@RecordApplicationEvents (1)
class OrderServiceTests {

	@Autowired
	OrderService orderService;

	@Autowired
	ApplicationEvents events; (2)

	@Test
	void submitOrder() {
		// Invoke method in OrderService that publishes an event
		orderService.submitOrder(new Order(/* ... */));
		// Verify that an OrderSubmitted event was published
		long numEvents = events.stream(OrderSubmitted.class).count(); (3)
		assertThat(numEvents).isEqualTo(1);
	}
}
1 使用 @RecordApplicationEvents 注释测试类。
2 Annotate the test class with @RecordApplicationEvents.
3 注入当前测试的 ApplicationEvents 实例。
4 Inject the ApplicationEvents instance for the current test.
5 使用 ApplicationEvents API 来计数已发布的 OrderSubmitted 事件。
6 Use the ApplicationEvents API to count how many OrderSubmitted events were published.
@SpringJUnitConfig(/* ... */)
@RecordApplicationEvents (1)
class OrderServiceTests {

	@Autowired
	lateinit var orderService: OrderService

	@Autowired
	lateinit var events: ApplicationEvents (2)

	@Test
	fun submitOrder() {
		// Invoke method in OrderService that publishes an event
		orderService.submitOrder(Order(/* ... */))
		// Verify that an OrderSubmitted event was published
		val numEvents = events.stream(OrderSubmitted::class).count() (3)
		assertThat(numEvents).isEqualTo(1)
	}
}
1 使用 @RecordApplicationEvents 注释测试类。
2 Annotate the test class with @RecordApplicationEvents.
3 注入当前测试的 ApplicationEvents 实例。
4 Inject the ApplicationEvents instance for the current test.
5 使用 ApplicationEvents API 来计数已发布的 OrderSubmitted 事件。
6 Use the ApplicationEvents API to count how many OrderSubmitted events were published.

另请参阅https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/context/event/ApplicationEvents.html[ApplicationEvents`javadoc],以了解有关 `ApplicationEvents API 的更多详细信息。

See the ApplicationEvents javadoc for further details regarding the ApplicationEvents API.