From 353e092bf6a71231d0e08d0127fe680462b583a7 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sat, 6 Apr 2019 15:33:47 +0200 Subject: [PATCH] Register EventPublishingTestExecutionListener by default (round 2) This commit registers the EventPublishingTestExecutionListener as a default TestExecutionListener with an order of 10,000. This registers the EventPublishingTestExecutionListener as the last listener provided by the Spring Framework. With EventPublishingTestExecutionListener registered with an order of 10,000, it is effectively wrapped by all other Spring listeners, including support for @DirtiesContext and test-managed transactions. Furthermore, this commit revises the implementation of EventPublishingTestExecutionListener to take advantage of the new TestContext#hasApplicationContext() support which allows the EventPublishingTestExecutionListener to publish events only if the test's ApplicationContext is currently available. This avoids undesirable side-effects such as eager loading of the ApplicationContext before it is needed or re-loading of the ApplicationContext after it has been intentionally closed. Closes gh-18490 --- .../test/context/TestExecutionListener.java | 4 +- .../EventPublishingTestExecutionListener.java | 60 ++++++------- .../main/resources/META-INF/spring.factories | 3 +- .../context/TestExecutionListenersTests.java | 13 +-- .../cache/ClassLevelDirtiesContextTests.java | 16 +++- .../DirtiesContextInterfaceTests.java | 16 +++- ...TestExecutionListenerIntegrationTests.java | 2 - ...tPublishingTestExecutionListenerTests.java | 85 +++++++++++++++---- src/docs/asciidoc/testing.adoc | 20 ++--- 9 files changed, 148 insertions(+), 71 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/TestExecutionListener.java b/spring-test/src/main/java/org/springframework/test/context/TestExecutionListener.java index ef5f0ab3ca8..815f1940d39 100644 --- a/spring-test/src/main/java/org/springframework/test/context/TestExecutionListener.java +++ b/spring-test/src/main/java/org/springframework/test/context/TestExecutionListener.java @@ -44,8 +44,6 @@ package org.springframework.test.context; *

Spring provides the following out-of-the-box implementations (all of * which implement {@code Ordered}): *

* * @author Sam Brannen diff --git a/spring-test/src/main/java/org/springframework/test/context/event/EventPublishingTestExecutionListener.java b/spring-test/src/main/java/org/springframework/test/context/event/EventPublishingTestExecutionListener.java index ed8812783a6..1ac348f0f94 100644 --- a/spring-test/src/main/java/org/springframework/test/context/event/EventPublishingTestExecutionListener.java +++ b/spring-test/src/main/java/org/springframework/test/context/event/EventPublishingTestExecutionListener.java @@ -16,15 +16,19 @@ package org.springframework.test.context.event; -import org.springframework.core.Ordered; +import java.util.function.Function; + import org.springframework.test.context.TestContext; import org.springframework.test.context.TestExecutionListener; import org.springframework.test.context.support.AbstractTestExecutionListener; /** * {@link org.springframework.test.context.TestExecutionListener TestExecutionListener} - * that publishes test execution events to a Spring test - * {@link org.springframework.context.ApplicationContext ApplicationContext}. + * that publishes test execution events to the + * {@link org.springframework.context.ApplicationContext ApplicationContext} + * for the currently executing test. Events are only published if the + * {@code ApplicationContext} {@linkplain TestContext#hasApplicationContext() + * has already been loaded}. * *

Supported Events

*