Introduce publishEvent() convenience method in TestContext
This commit introduces a publishEvent() method in the TestContext API
as a convenience for publishing an ApplicationEvent to the test's
ApplicationContext but only if the ApplicationContext is currently
available and with lazy creation of the ApplicationEvent.
For example, the beforeTestClass() method in
EventPublishingTestExecutionListener is now implemented as follows.
public void beforeTestClass(TestContext testContext) {
testContext.publishEvent(BeforeTestClassEvent::new);
}
Closes gh-22765
This commit is contained in:
@@ -18,8 +18,10 @@ package org.springframework.test.context;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.core.AttributeAccessor;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.test.annotation.DirtiesContext.HierarchyMode;
|
||||
@@ -76,6 +78,23 @@ public interface TestContext extends AttributeAccessor, Serializable {
|
||||
*/
|
||||
ApplicationContext getApplicationContext();
|
||||
|
||||
/**
|
||||
* Publish the {@link ApplicationEvent} created by the given {@code eventFactory}
|
||||
* to the {@linkplain ApplicationContext application context} for this
|
||||
* test context.
|
||||
* <p>The {@code ApplicationEvent} will only be published if the application
|
||||
* context for this test context {@linkplain #hasApplicationContext() is available}.
|
||||
* @param eventFactory factory for lazy creation of the {@code ApplicationEvent}
|
||||
* @since 5.2
|
||||
* @see #hasApplicationContext()
|
||||
* @see #getApplicationContext()
|
||||
*/
|
||||
default void publishEvent(Function<TestContext, ? extends ApplicationEvent> eventFactory) {
|
||||
if (hasApplicationContext()) {
|
||||
getApplicationContext().publishEvent(eventFactory.apply(this));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the {@linkplain Class test class} for this test context.
|
||||
* @return the test class (never {@code null})
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
package org.springframework.test.context.event;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.test.context.TestContext;
|
||||
import org.springframework.test.context.TestExecutionListener;
|
||||
import org.springframework.test.context.support.AbstractTestExecutionListener;
|
||||
@@ -92,7 +90,7 @@ public class EventPublishingTestExecutionListener extends AbstractTestExecutionL
|
||||
*/
|
||||
@Override
|
||||
public void beforeTestClass(TestContext testContext) {
|
||||
publishEvent(testContext, BeforeTestClassEvent::new);
|
||||
testContext.publishEvent(BeforeTestClassEvent::new);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -101,7 +99,7 @@ public class EventPublishingTestExecutionListener extends AbstractTestExecutionL
|
||||
*/
|
||||
@Override
|
||||
public void prepareTestInstance(TestContext testContext) {
|
||||
publishEvent(testContext, PrepareTestInstanceEvent::new);
|
||||
testContext.publishEvent(PrepareTestInstanceEvent::new);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -110,7 +108,7 @@ public class EventPublishingTestExecutionListener extends AbstractTestExecutionL
|
||||
*/
|
||||
@Override
|
||||
public void beforeTestMethod(TestContext testContext) {
|
||||
publishEvent(testContext, BeforeTestMethodEvent::new);
|
||||
testContext.publishEvent(BeforeTestMethodEvent::new);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -119,7 +117,7 @@ public class EventPublishingTestExecutionListener extends AbstractTestExecutionL
|
||||
*/
|
||||
@Override
|
||||
public void beforeTestExecution(TestContext testContext) {
|
||||
publishEvent(testContext, BeforeTestExecutionEvent::new);
|
||||
testContext.publishEvent(BeforeTestExecutionEvent::new);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -128,7 +126,7 @@ public class EventPublishingTestExecutionListener extends AbstractTestExecutionL
|
||||
*/
|
||||
@Override
|
||||
public void afterTestExecution(TestContext testContext) {
|
||||
publishEvent(testContext, AfterTestExecutionEvent::new);
|
||||
testContext.publishEvent(AfterTestExecutionEvent::new);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -137,7 +135,7 @@ public class EventPublishingTestExecutionListener extends AbstractTestExecutionL
|
||||
*/
|
||||
@Override
|
||||
public void afterTestMethod(TestContext testContext) {
|
||||
publishEvent(testContext, AfterTestMethodEvent::new);
|
||||
testContext.publishEvent(AfterTestMethodEvent::new);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -146,13 +144,7 @@ public class EventPublishingTestExecutionListener extends AbstractTestExecutionL
|
||||
*/
|
||||
@Override
|
||||
public void afterTestClass(TestContext testContext) {
|
||||
publishEvent(testContext, AfterTestClassEvent::new);
|
||||
}
|
||||
|
||||
private void publishEvent(TestContext testContext, Function<TestContext, TestContextEvent> eventFactory) {
|
||||
if (testContext.hasApplicationContext()) {
|
||||
testContext.getApplicationContext().publishEvent(eventFactory.apply(testContext));
|
||||
}
|
||||
testContext.publishEvent(AfterTestClassEvent::new);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user