Skip to content

Commit 19d5ec6

Browse files
committed
Improve documentation for ApplicationEvents to clarify recommended usage
See gh-35335
1 parent c0b71f8 commit 19d5ec6

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

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

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,19 @@ 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
2734
{assertj-docs}[AssertJ] to assert the types of application events published while
@@ -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: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,20 @@
3434
* {@link org.springframework.test.context.TestExecutionListeners @TestExecutionListeners}
3535
* that does not include the default listeners.</li>
3636
* <li>With JUnit Jupiter, declare a parameter of type {@code ApplicationEvents}
37-
* in a test or lifecycle method. Since {@code ApplicationEvents} is scoped to the
38-
* lifecycle of the current test method, this is the recommended approach.</li>
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>
3940
* <li>Alternatively, you can annotate a field of type {@code ApplicationEvents} with
4041
* {@link org.springframework.beans.factory.annotation.Autowired @Autowired} and
41-
* use that instance of {@code ApplicationEvents} in your test and lifecycle methods.
42-
* Note that {@code ApplicationEvents} is not a general Spring bean and is specifically
43-
* designed for use within test methods.</li>
42+
* use that instance of {@code ApplicationEvents} in your test and lifecycle methods.</li>
4443
* </ul>
4544
*
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+
*
4651
* @author Sam Brannen
4752
* @author Oliver Drotbohm
4853
* @since 5.3.3

0 commit comments

Comments
 (0)