Interface ApplicationEvents
public interface ApplicationEvents
ApplicationEvents
encapsulates all application events that were fired during the execution of a single test method.
To use ApplicationEvents
in your tests, do the following.
- Ensure that your test class is annotated or meta-annotated with
@RecordApplicationEvents
. - Ensure that the
ApplicationEventsTestExecutionListener
is registered. Note, however, that it 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. - With JUnit Jupiter, declare a parameter of type
ApplicationEvents
in a@Test
,@BeforeEach
, or@AfterEach
method. SinceApplicationEvents
is scoped to the lifecycle of the current test method, this is the recommended approach. - Alternatively, you can annotate a field of type
ApplicationEvents
with@Autowired
and use that instance ofApplicationEvents
in your test and lifecycle methods.
NOTE: ApplicationEvents
is registered with the ApplicationContext
as a
resolvable dependency which is scoped to the lifecycle of the current test method.
Consequently, ApplicationEvents
cannot be accessed outside the lifecycle of a
test method and cannot be @Autowired
into the constructor of a test class.
- Since:
- 5.3.3
- Author:
- Sam Brannen, Oliver Drotbohm
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Clear all application events recorded by thisApplicationEvents
instance.stream()
Stream all application events that were fired during test execution.<T> Stream<T>
Stream all application events or event payloads of the given type that were fired during test execution.
-
Method Details
-
stream
Stream<ApplicationEvent> stream()Stream all application events that were fired during test execution.- Returns:
- a stream of all application events
- See Also:
-
stream
Stream all application events or event payloads of the given type that were fired during test execution. -
clear
void clear()Clear all application events recorded by thisApplicationEvents
instance.Subsequent calls to
stream()
orstream(Class)
will only include events recorded since this method was invoked.- See Also:
-