Expose order values of TestExecutionListener implementations as constants

Prior to this commit, the order values of TestExecutionListener
implementations were hard-coded in their getOrder() methods.

To benefit users and integrators, this commit exposes those order values
as an ORDER constant in each TestExecutionListener.

See gh-34225
Closes gh-34404
This commit is contained in:
Sam Brannen
2025-02-11 11:53:14 +01:00
parent 9d3374b28d
commit 9797bc0acd
12 changed files with 99 additions and 25 deletions

View File

@@ -37,7 +37,13 @@ import org.springframework.test.context.support.DependencyInjectionTestExecution
public class BeanOverrideTestExecutionListener extends AbstractTestExecutionListener {
/**
* Returns {@code 1950}, which ensures that the {@code BeanOverrideTestExecutionListener}
* The {@link #getOrder() order} value for this listener: {@value}.
* @since 6.2.3
*/
public static final int ORDER = 1950;
/**
* Returns {@value #ORDER}, which ensures that the {@code BeanOverrideTestExecutionListener}
* is ordered after the
* {@link org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener
* DirtiesContextBeforeModesTestExecutionListener} and before the
@@ -45,7 +51,7 @@ public class BeanOverrideTestExecutionListener extends AbstractTestExecutionList
*/
@Override
public int getOrder() {
return 1950;
return ORDER;
}
/**

View File

@@ -49,6 +49,13 @@ import org.springframework.util.ClassUtils;
*/
public class MockitoResetTestExecutionListener extends AbstractTestExecutionListener {
/**
* The {@link #getOrder() order} value for this listener
* ({@code Ordered.LOWEST_PRECEDENCE - 100}): {@value}.
* @since 6.2.3
*/
public static final int ORDER = Ordered.LOWEST_PRECEDENCE - 100;
private static final Log logger = LogFactory.getLog(MockitoResetTestExecutionListener.class);
/**
@@ -74,9 +81,10 @@ public class MockitoResetTestExecutionListener extends AbstractTestExecutionList
/**
* Returns {@code Ordered.LOWEST_PRECEDENCE - 100}, which ensures that the
* Returns {@value #ORDER}, which ensures that the
* {@code MockitoResetTestExecutionListener} is ordered after all standard
* {@code TestExecutionListener} implementations.
* @see #ORDER
*/
@Override
public int getOrder() {

View File

@@ -48,6 +48,12 @@ import org.springframework.util.Assert;
*/
public class ApplicationEventsTestExecutionListener extends AbstractTestExecutionListener {
/**
* The {@link #getOrder() order} value for this listener: {@value}.
* @since 6.2.3
*/
public static final int ORDER = 1800;
/**
* Attribute name for a {@link TestContext} attribute which indicates
* whether the test class for the given test context is annotated with
@@ -61,7 +67,7 @@ public class ApplicationEventsTestExecutionListener extends AbstractTestExecutio
/**
* Returns {@code 1800}, which ensures that the {@code ApplicationEventsTestExecutionListener}
* Returns {@value #ORDER}, which ensures that the {@code ApplicationEventsTestExecutionListener}
* is ordered after the
* {@link org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener
* DirtiesContextBeforeModesTestExecutionListener} and before the
@@ -72,7 +78,7 @@ public class ApplicationEventsTestExecutionListener extends AbstractTestExecutio
*/
@Override
public final int getOrder() {
return 1800;
return ORDER;
}
@Override

View File

@@ -98,7 +98,13 @@ import org.springframework.test.context.support.AbstractTestExecutionListener;
public class EventPublishingTestExecutionListener extends AbstractTestExecutionListener {
/**
* Returns {@code 10000}, which ensures that the {@code EventPublishingTestExecutionListener}
* The {@link #getOrder() order} value for this listener: {@value}.
* @since 6.2.3
*/
public static final int ORDER = 10_000;
/**
* Returns {@value #ORDER}, which ensures that the {@code EventPublishingTestExecutionListener}
* is ordered after the
* {@link org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener
* SqlScriptsTestExecutionListener} and before the
@@ -107,7 +113,7 @@ public class EventPublishingTestExecutionListener extends AbstractTestExecutionL
*/
@Override
public final int getOrder() {
return 10_000;
return ORDER;
}
/**

View File

@@ -117,6 +117,12 @@ import static org.springframework.util.ResourceUtils.CLASSPATH_URL_PREFIX;
*/
public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListener implements AotTestExecutionListener {
/**
* The {@link #getOrder() order} value for this listener: {@value}.
* @since 6.2.3
*/
public static final int ORDER = 5000;
private static final String SLASH = "/";
private static final Log logger = LogFactory.getLog(SqlScriptsTestExecutionListener.class);
@@ -126,7 +132,7 @@ public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListen
/**
* Returns {@code 5000}, which ensures that the {@code SqlScriptsTestExecutionListener}
* Returns {@value #ORDER}, which ensures that the {@code SqlScriptsTestExecutionListener}
* is ordered after the
* {@link org.springframework.test.context.transaction.TransactionalTestExecutionListener
* TransactionalTestExecutionListener} and before the
@@ -135,7 +141,7 @@ public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListen
*/
@Override
public final int getOrder() {
return 5000;
return ORDER;
}
/**

View File

@@ -44,6 +44,12 @@ import org.springframework.util.ReflectionUtils;
*/
class MicrometerObservationRegistryTestExecutionListener extends AbstractTestExecutionListener {
/**
* The {@link #getOrder() order} value for this listener: {@value}.
* @since 6.2.3
*/
public static final int ORDER = 2500;
private static final Log logger = LogFactory.getLog(MicrometerObservationRegistryTestExecutionListener.class);
/**
@@ -107,7 +113,7 @@ class MicrometerObservationRegistryTestExecutionListener extends AbstractTestExe
/**
* Returns {@code 2500}, which ensures that the
* Returns {@value #ORDER}, which ensures that the
* {@code MicrometerObservationRegistryTestExecutionListener} is ordered after the
* {@link org.springframework.test.context.support.DependencyInjectionTestExecutionListener
* DependencyInjectionTestExecutionListener} and before the
@@ -116,7 +122,7 @@ class MicrometerObservationRegistryTestExecutionListener extends AbstractTestExe
*/
@Override
public final int getOrder() {
return 2500;
return ORDER;
}
/**

View File

@@ -36,7 +36,14 @@ import org.springframework.test.context.TestContext;
public class CommonCachesTestExecutionListener extends AbstractTestExecutionListener {
/**
* Returns {@code 3005}, which ensures that the {@code CommonCachesTestExecutionListener}
* The {@link #getOrder() order} value for this listener: {@value}.
* @since 6.2.3
*/
public static final int ORDER = 3005;
/**
* Returns {@value #ORDER}, which ensures that the {@code CommonCachesTestExecutionListener}
* is ordered after the
* {@link DirtiesContextTestExecutionListener DirtiesContextTestExecutionListener} and before the
* {@link org.springframework.test.context.transaction.TransactionalTestExecutionListener
@@ -44,10 +51,9 @@ public class CommonCachesTestExecutionListener extends AbstractTestExecutionList
*/
@Override
public final int getOrder() {
return 3005;
return ORDER;
}
@Override
public void afterTestClass(TestContext testContext) throws Exception {
if (testContext.hasApplicationContext()) {

View File

@@ -39,6 +39,12 @@ import org.springframework.test.context.aot.AotTestContextInitializers;
*/
public class DependencyInjectionTestExecutionListener extends AbstractTestExecutionListener {
/**
* The {@link #getOrder() order} value for this listener: {@value}.
* @since 6.2.3
*/
public static final int ORDER = 2000;
/**
* Attribute name for a {@link TestContext} attribute which indicates
* whether the dependencies of a test instance should be
@@ -63,7 +69,7 @@ public class DependencyInjectionTestExecutionListener extends AbstractTestExecut
/**
* Returns {@code 2000}, which ensures that the {@code DependencyInjectionTestExecutionListener}
* Returns {@value #ORDER}, which ensures that the {@code DependencyInjectionTestExecutionListener}
* is ordered after the
* {@link DirtiesContextBeforeModesTestExecutionListener DirtiesContextBeforeModesTestExecutionListener}
* and the {@link org.springframework.test.context.bean.override.BeanOverrideTestExecutionListener
@@ -74,7 +80,7 @@ public class DependencyInjectionTestExecutionListener extends AbstractTestExecut
*/
@Override
public final int getOrder() {
return 2000;
return ORDER;
}
/**

View File

@@ -55,8 +55,14 @@ import static org.springframework.test.annotation.DirtiesContext.MethodMode.BEFO
public class DirtiesContextBeforeModesTestExecutionListener extends AbstractDirtiesContextTestExecutionListener {
/**
* Returns {@code 1500}, which ensures that the {@code DirtiesContextBeforeModesTestExecutionListener}
* is ordered after the
* The {@link #getOrder() order} value for this listener: {@value}.
* @since 6.2.3
*/
public static final int ORDER = 1500;
/**
* Returns {@value #ORDER}, which ensures that the
* {@code DirtiesContextBeforeModesTestExecutionListener} is ordered after the
* {@link org.springframework.test.context.web.ServletTestExecutionListener
* ServletTestExecutionListener} and before the
* {@link org.springframework.test.context.event.ApplicationEventsTestExecutionListener
@@ -66,7 +72,7 @@ public class DirtiesContextBeforeModesTestExecutionListener extends AbstractDirt
*/
@Override
public final int getOrder() {
return 1500;
return ORDER;
}
/**

View File

@@ -55,14 +55,20 @@ import static org.springframework.test.annotation.DirtiesContext.MethodMode.AFTE
public class DirtiesContextTestExecutionListener extends AbstractDirtiesContextTestExecutionListener {
/**
* Returns {@code 3000}, which ensures that the {@code DirtiesContextTestExecutionListener}
* The {@link #getOrder() order} value for this listener: {@value}.
* @since 6.2.3
*/
public static final int ORDER = 3000;
/**
* Returns {@value #ORDER}, which ensures that the {@code DirtiesContextTestExecutionListener}
* is ordered after the
* {@link DependencyInjectionTestExecutionListener DependencyInjectionTestExecutionListener}
* and before the {@link CommonCachesTestExecutionListener CommonCachesTestExecutionListener}.
*/
@Override
public final int getOrder() {
return 3000;
return ORDER;
}
/**

View File

@@ -148,6 +148,12 @@ import org.springframework.util.StringUtils;
*/
public class TransactionalTestExecutionListener extends AbstractTestExecutionListener {
/**
* The {@link #getOrder() order} value for this listener: {@value}.
* @since 6.2.3
*/
public static final int ORDER = 4000;
private static final Log logger = LogFactory.getLog(TransactionalTestExecutionListener.class);
// Do not require @Transactional test methods to be public.
@@ -177,7 +183,7 @@ public class TransactionalTestExecutionListener extends AbstractTestExecutionLis
/**
* Returns {@code 4000}, which ensures that the {@code TransactionalTestExecutionListener}
* Returns {@value #ORDER}, which ensures that the {@code TransactionalTestExecutionListener}
* is ordered after the
* {@link org.springframework.test.context.support.CommonCachesTestExecutionListener
* CommonCachesTestExecutionListener} and before the
@@ -186,7 +192,7 @@ public class TransactionalTestExecutionListener extends AbstractTestExecutionLis
*/
@Override
public final int getOrder() {
return 4000;
return ORDER;
}
/**

View File

@@ -64,6 +64,12 @@ import org.springframework.web.context.request.ServletWebRequest;
*/
public class ServletTestExecutionListener extends AbstractTestExecutionListener {
/**
* The {@link #getOrder() order} value for this listener: {@value}.
* @since 6.2.3
*/
public static final int ORDER = 1000;
/**
* Attribute name for a {@link TestContext} attribute which indicates
* whether the {@code ServletTestExecutionListener} should {@linkplain
@@ -110,14 +116,14 @@ public class ServletTestExecutionListener extends AbstractTestExecutionListener
/**
* Returns {@code 1000}, which ensures that the {@code ServletTestExecutionListener}
* Returns {@value #ORDER}, which ensures that the {@code ServletTestExecutionListener}
* is ordered before the
* {@link org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener
* DirtiesContextBeforeModesTestExecutionListener}.
*/
@Override
public final int getOrder() {
return 1000;
return ORDER;
}
/**