Skip to content

Commit 472f844

Browse files
committed
Merge branch '6.2.x'
2 parents 208bb48 + 19d5ec6 commit 472f844

File tree

2 files changed

+34
-32
lines changed

2 files changed

+34
-32
lines changed

framework-docs/modules/ROOT/pages/testing/testcontext-framework/application-events.adoc

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
= Application Events
33

44
The TestContext framework provides support for recording
5-
xref:core/beans/context-introduction.adoc#context-functionality-events[application events] published in the
6-
`ApplicationContext` so that assertions can be performed against those events within
7-
tests. All events published during the execution of a single test are made available via
8-
the `ApplicationEvents` API which allows you to process the events as a
5+
xref:core/beans/context-introduction.adoc#context-functionality-events[application events]
6+
published in the `ApplicationContext` so that assertions can be performed against those
7+
events within tests. All events published during the execution of a single test are made
8+
available via the `ApplicationEvents` API which allows you to process the events as a
99
`java.util.Stream`.
1010

1111
To use `ApplicationEvents` in your tests, do the following.
@@ -16,16 +16,23 @@ To use `ApplicationEvents` in your tests, do the following.
1616
that `ApplicationEventsTestExecutionListener` is registered by default and only needs
1717
to be manually registered if you have custom configuration via
1818
`@TestExecutionListeners` that does not include the default listeners.
19-
* Annotate a field of type `ApplicationEvents` with `@Autowired` and use that instance of
20-
`ApplicationEvents` in your test and lifecycle methods (such as `@BeforeEach` and
21-
`@AfterEach` methods in JUnit Jupiter).
22-
** When using the xref:testing/testcontext-framework/support-classes.adoc#testcontext-junit-jupiter-extension[SpringExtension for JUnit Jupiter], you may declare a method
23-
parameter of type `ApplicationEvents` in a test or lifecycle method as an alternative
24-
to an `@Autowired` field in the test class.
19+
* When using the
20+
xref:testing/testcontext-framework/support-classes.adoc#testcontext-junit-jupiter-extension[SpringExtension for JUnit Jupiter],
21+
declare a method parameter of type `ApplicationEvents` in a `@Test`, `@BeforeEach`, or
22+
`@AfterEach` method.
23+
** Since `ApplicationEvents` is scoped to the lifecycle of the current test method, this
24+
is the recommended approach.
25+
* Alternatively, you can annotate a field of type `ApplicationEvents` with `@Autowired`
26+
and use that instance of `ApplicationEvents` in your test and lifecycle methods.
27+
28+
NOTE: `ApplicationEvents` is registered with the `ApplicationContext` as a _resolvable
29+
dependency_ which is scoped to the lifecycle of the current test method. Consequently,
30+
`ApplicationEvents` cannot be accessed outside the lifecycle of a test method and cannot be
31+
`@Autowired` into the constructor of a test class.
2532

2633
The following test class uses the `SpringExtension` for JUnit Jupiter and
27-
{assertj-docs}[AssertJ] to assert the types of application events
28-
published while invoking a method in a Spring-managed component:
34+
{assertj-docs}[AssertJ] to assert the types of application events published while
35+
invoking a method in a Spring-managed component:
2936

3037
// Don't use "quotes" in the "subs" section because of the asterisks in /* ... */
3138
[tabs]
@@ -38,16 +45,10 @@ Java::
3845
@RecordApplicationEvents // <1>
3946
class OrderServiceTests {
4047
41-
@Autowired
42-
OrderService orderService;
43-
44-
@Autowired
45-
ApplicationEvents events; // <2>
46-
4748
@Test
48-
void submitOrder() {
49+
void submitOrder(@Autowired OrderService service, ApplicationEvents events) { // <2>
4950
// Invoke method in OrderService that publishes an event
50-
orderService.submitOrder(new Order(/* ... */));
51+
service.submitOrder(new Order(/* ... */));
5152
// Verify that an OrderSubmitted event was published
5253
long numEvents = events.stream(OrderSubmitted.class).count(); // <3>
5354
assertThat(numEvents).isEqualTo(1);
@@ -66,16 +67,10 @@ Kotlin::
6667
@RecordApplicationEvents // <1>
6768
class OrderServiceTests {
6869
69-
@Autowired
70-
lateinit var orderService: OrderService
71-
72-
@Autowired
73-
lateinit var events: ApplicationEvents // <2>
74-
7570
@Test
76-
fun submitOrder() {
71+
fun submitOrder(@Autowired service: OrderService, events: ApplicationEvents) { // <2>
7772
// Invoke method in OrderService that publishes an event
78-
orderService.submitOrder(Order(/* ... */))
73+
service.submitOrder(Order(/* ... */))
7974
// Verify that an OrderSubmitted event was published
8075
val numEvents = events.stream(OrderSubmitted::class).count() // <3>
8176
assertThat(numEvents).isEqualTo(1)

spring-test/src/main/java/org/springframework/test/context/event/ApplicationEvents.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,21 @@
3333
* to be manually registered if you have custom configuration via
3434
* {@link org.springframework.test.context.TestExecutionListeners @TestExecutionListeners}
3535
* that does not include the default listeners.</li>
36-
* <li>Annotate a field of type {@code ApplicationEvents} with
36+
* <li>With JUnit Jupiter, declare a parameter of type {@code ApplicationEvents}
37+
* in a {@code @Test}, {@code @BeforeEach}, or {@code @AfterEach} method. Since
38+
* {@code ApplicationEvents} is scoped to the lifecycle of the current test method,
39+
* this is the recommended approach.</li>
40+
* <li>Alternatively, you can annotate a field of type {@code ApplicationEvents} with
3741
* {@link org.springframework.beans.factory.annotation.Autowired @Autowired} and
3842
* use that instance of {@code ApplicationEvents} in your test and lifecycle methods.</li>
39-
* <li>With JUnit Jupiter, you may optionally declare a parameter of type
40-
* {@code ApplicationEvents} in a test or lifecycle method as an alternative to
41-
* an {@code @Autowired} field in the test class.</li>
4243
* </ul>
4344
*
45+
* <p>NOTE: {@code ApplicationEvents} is registered with the {@code ApplicationContext} as a
46+
* {@linkplain org.springframework.beans.factory.config.ConfigurableListableBeanFactory#registerResolvableDependency
47+
* resolvable dependency} which is scoped to the lifecycle of the current test method.
48+
* Consequently, {@code ApplicationEvents} cannot be accessed outside the lifecycle of a
49+
* test method and cannot be {@code @Autowired} into the constructor of a test class.
50+
*
4451
* @author Sam Brannen
4552
* @author Oliver Drotbohm
4653
* @since 5.3.3

0 commit comments

Comments
 (0)