You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
9
9
`java.util.Stream`.
10
10
11
11
To use `ApplicationEvents` in your tests, do the following.
@@ -16,16 +16,23 @@ To use `ApplicationEvents` in your tests, do the following.
16
16
that `ApplicationEventsTestExecutionListener` is registered by default and only needs
17
17
to be manually registered if you have custom configuration via
18
18
`@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.
25
32
26
33
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:
29
36
30
37
// Don't use "quotes" in the "subs" section because of the asterisks in /* ... */
0 commit comments