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
Copy file name to clipboardExpand all lines: framework-docs/modules/ROOT/pages/testing/testcontext-framework/application-events.adoc
+17-22Lines changed: 17 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,12 +16,19 @@ 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
34
{assertj-docs}[AssertJ] to assert the types of application events published while
0 commit comments