PublishedEventsExtension
registers a composite ApplicationListener
backed by thread-bound individual listeners to capture all application events published during the execution of a test method. Those would declare a PublishedEvents
parameter to the method which provides API to define assertions based on the events published:
@ExtendWith(PublishedEventsExtension.class)
class SampleTests {
@Test
void someTestMethod(PublishedEvents events) {
// Filter events by type and predicate
assertThat(events.ofType(MyEventType.class).matching(it -> …)).hasSize(2);
}
}
Comment From: sbrannen
Thanks for the PR!
Tentatively slated for 5.3 RC1 for review and potential inclusion in 5.3
Comment From: sbrannen
The current proposal introduces support for the following abstraction.
/**
* {@code ApplicationEvents} encapsulates all {@linkplain ApplicationEvent
* application events} that were fired during the execution of a single test
* method.
*
* @author Sam Brannen
* @author Oliver Drotbohm
* @since 5.3.1
* @see ApplicationEventsExtension
* @see org.springframework.context.ApplicationEvent
* @see org.springframework.context.ApplicationListener
*/
public interface ApplicationEvents {
/**
* Stream all application events that were fired during test execution.
* @return a stream of all application events
*/
Stream<ApplicationEvent> stream();
/**
* Stream all application events or event payloads of the given type that
* were fired during test execution.
* @param <T> the event type
* @param type the type of events or payloads to stream; never {@code null}
* @return a stream of all application events or event payloads of the
* specified type
*/
<T> Stream<T> stream(Class<T> type);
}
Current work on this issue can be viewed in the following branch that builds on top of this PR: https://github.com/spring-projects/spring-framework/compare/master...sbrannen:issues/gh-25616-application-events-extension
Tentatively slated for inclusion in 5.3.2
.
Comment From: odrotbohm
Lovely! Thanks for all the extra effort you put into this, @sbrannen! 🙇
Comment From: sbrannen
You're very welcome!
Thanks for the idea, the prototype, and for your patience. :)