Enable default TestExecutionListeners in JUnit 4 and TestNG base test classes

Prior to this commit, the abstract base test classes for JUnit 4 and
TestNG registered explicit sets of TestExecutionListeners. This
configuration was useful in the early years of the Spring TestContext
Framework. However, since the introduction of support for automatic
registration of "default" listeners in Spring Framework 4.1, these
predefined sets of TestExecutionListeners have become a hindrance to
using Spring's testing support with default listeners from Spring
portfolio projects (such as Spring Boot and Spring Security) as well as
third-party or project-specific default listeners.

To address this issue, the four abstract base test classes for JUnit 4
and TestNG no longer declare listeners via @TestExecutionListeners and
instead now rely on registration of default listeners.

Closes gh-29149
This commit is contained in:
Sam Brannen
2022-10-08 16:30:02 +02:00
parent 3061a81b0f
commit d4a5565340
4 changed files with 4 additions and 130 deletions

View File

@@ -27,12 +27,6 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestContextManager;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.event.ApplicationEventsTestExecutionListener;
import org.springframework.test.context.event.EventPublishingTestExecutionListener;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import org.springframework.test.context.web.ServletTestExecutionListener;
/**
* Abstract base test class which integrates the <em>Spring TestContext
@@ -43,25 +37,7 @@ import org.springframework.test.context.web.ServletTestExecutionListener;
* {@link ContextConfiguration @ContextConfiguration} annotation to
* configure the {@linkplain ApplicationContext application context} {@linkplain
* ContextConfiguration#locations() resource locations} or {@linkplain
* ContextConfiguration#classes() component classes}. <em>If your test does not
* need to load an application context, you may choose to omit the
* {@link ContextConfiguration @ContextConfiguration} declaration and to configure
* the appropriate {@link org.springframework.test.context.TestExecutionListener
* TestExecutionListeners} manually.</em>
*
* <p>This class explicitly registers the following {@code TestExecutionListener}
* implementations. If you want to switch to using the <em>default</em> set of
* listeners, see the class-level Javadoc for
* {@link TestExecutionListeners @TestExecutionListeners} for details.
*
* <ul>
* <li>{@link org.springframework.test.context.web.ServletTestExecutionListener}
* <li>{@link org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener}
* <li>{@link org.springframework.test.context.event.ApplicationEventsTestExecutionListener}
* <li>{@link org.springframework.test.context.support.DependencyInjectionTestExecutionListener}
* <li>{@link org.springframework.test.context.support.DirtiesContextTestExecutionListener}
* <li>{@link org.springframework.test.context.event.EventPublishingTestExecutionListener}
* </ul>
* ContextConfiguration#classes() component classes}.
*
* <p>This class serves only as a convenience for extension.
* <ul>
@@ -84,19 +60,10 @@ import org.springframework.test.context.web.ServletTestExecutionListener;
* @see TestContext
* @see TestContextManager
* @see TestExecutionListeners
* @see ServletTestExecutionListener
* @see DirtiesContextBeforeModesTestExecutionListener
* @see ApplicationEventsTestExecutionListener
* @see DependencyInjectionTestExecutionListener
* @see DirtiesContextTestExecutionListener
* @see EventPublishingTestExecutionListener
* @see AbstractTransactionalJUnit4SpringContextTests
* @see org.springframework.test.context.testng.AbstractTestNGSpringContextTests
*/
@RunWith(SpringRunner.class)
@TestExecutionListeners({ ServletTestExecutionListener.class, DirtiesContextBeforeModesTestExecutionListener.class,
ApplicationEventsTestExecutionListener.class, DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class, EventPublishingTestExecutionListener.class })
public abstract class AbstractJUnit4SpringContextTests implements ApplicationContextAware {
/**

View File

@@ -27,14 +27,6 @@ import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.lang.Nullable;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.event.ApplicationEventsTestExecutionListener;
import org.springframework.test.context.event.EventPublishingTestExecutionListener;
import org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.test.context.web.ServletTestExecutionListener;
import org.springframework.test.jdbc.JdbcTestUtils;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional;
@@ -57,22 +49,6 @@ import org.springframework.util.Assert;
* <p>Concrete subclasses must fulfill the same requirements outlined in
* {@link AbstractJUnit4SpringContextTests}.
*
* <p>This class explicitly registers the following {@code TestExecutionListener}
* implementations. If you want to switch to using the <em>default</em> set of
* listeners, see the class-level Javadoc for
* {@link TestExecutionListeners @TestExecutionListeners} for details.
*
* <ul>
* <li>{@link org.springframework.test.context.web.ServletTestExecutionListener}
* <li>{@link org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener}
* <li>{@link org.springframework.test.context.event.ApplicationEventsTestExecutionListener}</li>
* <li>{@link org.springframework.test.context.support.DependencyInjectionTestExecutionListener}
* <li>{@link org.springframework.test.context.support.DirtiesContextTestExecutionListener}
* <li>{@link org.springframework.test.context.transaction.TransactionalTestExecutionListener}
* <li>{@link org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener}
* <li>{@link org.springframework.test.context.event.EventPublishingTestExecutionListener}
* </ul>
*
* <p>This class serves only as a convenience for extension.
* <ul>
* <li>If you do not wish for your test classes to be tied to a Spring-specific
@@ -94,8 +70,6 @@ import org.springframework.util.Assert;
* @see AbstractJUnit4SpringContextTests
* @see org.springframework.test.context.ContextConfiguration
* @see org.springframework.test.context.TestExecutionListeners
* @see org.springframework.test.context.transaction.TransactionalTestExecutionListener
* @see org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener
* @see org.springframework.transaction.annotation.Transactional
* @see org.springframework.test.annotation.Commit
* @see org.springframework.test.annotation.Rollback
@@ -104,10 +78,6 @@ import org.springframework.util.Assert;
* @see org.springframework.test.jdbc.JdbcTestUtils
* @see org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests
*/
@TestExecutionListeners(listeners = { ServletTestExecutionListener.class, DirtiesContextBeforeModesTestExecutionListener.class,
ApplicationEventsTestExecutionListener.class, DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class,
SqlScriptsTestExecutionListener.class, EventPublishingTestExecutionListener.class }, inheritListeners = false)
@Transactional
public abstract class AbstractTransactionalJUnit4SpringContextTests extends AbstractJUnit4SpringContextTests {

View File

@@ -36,12 +36,6 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestContextManager;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.event.ApplicationEventsTestExecutionListener;
import org.springframework.test.context.event.EventPublishingTestExecutionListener;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import org.springframework.test.context.web.ServletTestExecutionListener;
/**
* Abstract base test class which integrates the <em>Spring TestContext Framework</em>
@@ -52,26 +46,9 @@ import org.springframework.test.context.web.ServletTestExecutionListener;
* {@link ContextConfiguration @ContextConfiguration} annotation to
* configure the {@linkplain ApplicationContext application context} {@linkplain
* ContextConfiguration#locations() resource locations} or {@linkplain
* ContextConfiguration#classes() component classes}. <em>If your test does not
* need to load an application context, you may choose to omit the
* {@link ContextConfiguration @ContextConfiguration} declaration and to configure
* the appropriate {@link org.springframework.test.context.TestExecutionListener
* TestExecutionListeners} manually.</em> Concrete subclasses must also have
* constructors which either implicitly or explicitly delegate to {@code super();}.
*
* <p>This class explicitly registers the following {@code TestExecutionListener}
* implementations. If you want to switch to using the <em>default</em> set of
* listeners, see the class-level Javadoc for
* {@link TestExecutionListeners @TestExecutionListeners} for details.
*
* <ul>
* <li>{@link org.springframework.test.context.web.ServletTestExecutionListener}
* <li>{@link org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener}
* <li>{@link org.springframework.test.context.event.ApplicationEventsTestExecutionListener}
* <li>{@link org.springframework.test.context.support.DependencyInjectionTestExecutionListener}
* <li>{@link org.springframework.test.context.support.DirtiesContextTestExecutionListener}
* <li>{@link org.springframework.test.context.event.EventPublishingTestExecutionListener}
* </ul>
* ContextConfiguration#classes() component classes}. Concrete subclasses must
* also have constructors which either implicitly or explicitly delegate to
* {@code super();}.
*
* @author Sam Brannen
* @author Juergen Hoeller
@@ -80,18 +57,9 @@ import org.springframework.test.context.web.ServletTestExecutionListener;
* @see TestContext
* @see TestContextManager
* @see TestExecutionListeners
* @see ServletTestExecutionListener
* @see DirtiesContextBeforeModesTestExecutionListener
* @see ApplicationEventsTestExecutionListener
* @see DependencyInjectionTestExecutionListener
* @see DirtiesContextTestExecutionListener
* @see EventPublishingTestExecutionListener
* @see AbstractTransactionalTestNGSpringContextTests
* @see org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests
*/
@TestExecutionListeners({ ServletTestExecutionListener.class, DirtiesContextBeforeModesTestExecutionListener.class,
ApplicationEventsTestExecutionListener.class, DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class, EventPublishingTestExecutionListener.class })
public abstract class AbstractTestNGSpringContextTests implements IHookable, ApplicationContextAware {
/** Logger available to subclasses. */

View File

@@ -25,15 +25,6 @@ import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.lang.Nullable;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.event.ApplicationEventsTestExecutionListener;
import org.springframework.test.context.event.EventPublishingTestExecutionListener;
import org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.test.context.web.ServletTestExecutionListener;
import org.springframework.test.jdbc.JdbcTestUtils;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional;
@@ -56,30 +47,12 @@ import org.springframework.util.Assert;
* <p>Concrete subclasses must fulfill the same requirements outlined in
* {@link AbstractTestNGSpringContextTests}.
*
* <p>This class explicitly registers the following {@code TestExecutionListener}
* implementations. If you want to switch to using the <em>default</em> set of
* listeners, see the class-level Javadoc for
* {@link TestExecutionListeners @TestExecutionListeners} for details.
*
* <ul>
* <li>{@link org.springframework.test.context.web.ServletTestExecutionListener}
* <li>{@link org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener}
* <li>{@link org.springframework.test.context.event.ApplicationEventsTestExecutionListener}</li>
* <li>{@link org.springframework.test.context.support.DependencyInjectionTestExecutionListener}
* <li>{@link org.springframework.test.context.support.DirtiesContextTestExecutionListener}
* <li>{@link org.springframework.test.context.transaction.TransactionalTestExecutionListener}
* <li>{@link org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener}
* <li>{@link org.springframework.test.context.event.EventPublishingTestExecutionListener}
* </ul>
*
* @author Sam Brannen
* @author Juergen Hoeller
* @since 2.5
* @see AbstractTestNGSpringContextTests
* @see org.springframework.test.context.ContextConfiguration
* @see org.springframework.test.context.TestExecutionListeners
* @see org.springframework.test.context.transaction.TransactionalTestExecutionListener
* @see org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener
* @see org.springframework.transaction.annotation.Transactional
* @see org.springframework.test.annotation.Commit
* @see org.springframework.test.annotation.Rollback
@@ -88,10 +61,6 @@ import org.springframework.util.Assert;
* @see org.springframework.test.jdbc.JdbcTestUtils
* @see org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests
*/
@TestExecutionListeners(listeners = { ServletTestExecutionListener.class, DirtiesContextBeforeModesTestExecutionListener.class,
ApplicationEventsTestExecutionListener.class, DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class,
SqlScriptsTestExecutionListener.class, EventPublishingTestExecutionListener.class }, inheritListeners = false)
@Transactional
public abstract class AbstractTransactionalTestNGSpringContextTests extends AbstractTestNGSpringContextTests {