Status Quo
It has always been possible for a TestExecutionListener to publish an ApplicationEvent to the current test's ApplicationContext via testContext.getApplicationContext().publishEvent(new MyEvent()); however, that resulted in undesirable loading of the ApplicationContext in some cases.
Typically people only want to publish events if the ApplicationContext is currently available.
The testContext.hasApplicationContext() support introduced in gh-22756 makes it possible to only publish events if the ApplicationContext is currently available as follows.
if (testContext.hasApplicationContext()) {
testContext.getApplicationContext().publishEvent(new MyEvent());
}
This issue aims to introduce a convenience method in the TestContext API that performs the above with lazy creation of the actual ApplicationEvent.
Deliverables
Status Quo
It has always been possible for a
TestExecutionListenerto publish anApplicationEventto the current test'sApplicationContextviatestContext.getApplicationContext().publishEvent(new MyEvent()); however, that resulted in undesirable loading of theApplicationContextin some cases.Typically people only want to publish events if the
ApplicationContextis currently available.The
testContext.hasApplicationContext()support introduced in gh-22756 makes it possible to only publish events if theApplicationContextis currently available as follows.This issue aims to introduce a convenience method in the
TestContextAPI that performs the above with lazy creation of the actualApplicationEvent.Deliverables
publishEvent()convenience method inTestContext.