Skip to content

Commit a599601

Browse files
committed
Document how to switch to the default set of TestExecutionListeners
Closes gh-29281
1 parent 5eee467 commit a599601

File tree

3 files changed

+82
-16
lines changed

3 files changed

+82
-16
lines changed

spring-test/src/main/java/org/springframework/test/context/TestExecutionListener.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,8 +41,24 @@
4141
* {@link org.springframework.core.annotation.Order @Order} annotation. See
4242
* {@link TestContextBootstrapper#getTestExecutionListeners()} for details.
4343
*
44-
* <p>Spring provides the following out-of-the-box implementations (all of
45-
* which implement {@code Ordered}):
44+
* <h3>Registering TestExecutionListener Implementations</h3>
45+
*
46+
* <p>A {@code TestExecutionListener} can be registered explicitly for a test class,
47+
* its subclasses, and its nested classes by using the
48+
* {@link TestExecutionListeners @TestExecutionListeners} annotation. Explicit
49+
* registration is suitable for custom listeners that are used in limited testing
50+
* scenarios. However, it can become cumbersome if a custom listener needs to be
51+
* used across an entire test suite. This issue is addressed through support for
52+
* automatic discovery of <em>default</em> {@code TestExecutionListener}
53+
* implementations through the
54+
* {@link org.springframework.core.io.support.SpringFactoriesLoader SpringFactoriesLoader}
55+
* mechanism. Specifically, default {@code TestExecutionListener} implementations
56+
* can be registered under the {@code org.springframework.test.context.TestExecutionListener}
57+
* key in a {@code META-INF/spring.factories} properties file.
58+
*
59+
* <p>Spring provides the following implementations. Each of these implements
60+
* {@code Ordered} and is registered automatically by default.
61+
*
4662
* <ul>
4763
* <li>{@link org.springframework.test.context.web.ServletTestExecutionListener
4864
* ServletTestExecutionListener}</li>

spring-test/src/main/java/org/springframework/test/context/TestExecutionListeners.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,30 @@
3030
* which {@link TestExecutionListener TestExecutionListeners} should be
3131
* registered with a {@link TestContextManager}.
3232
*
33-
* <p>Typically, {@code @TestExecutionListeners} will be used in conjunction
34-
* with {@link ContextConfiguration @ContextConfiguration}.
33+
* <p>{@code @TestExecutionListeners} is used to register listeners for a
34+
* particular test class, its subclasses, and its nested classes. If you wish to
35+
* register a listener globally, you should register it via the automatic discovery
36+
* mechanism described in {@link TestExecutionListener}.
3537
*
3638
* <p>This annotation may be used as a <em>meta-annotation</em> to create custom
37-
* <em>composed annotations</em>.
38-
*
39-
* <p>As of Spring Framework 5.3, this annotation will be inherited from an
40-
* enclosing test class by default. See
39+
* <em>composed annotations</em>. As of Spring Framework 5.3, this annotation will
40+
* be inherited from an enclosing test class by default. See
4141
* {@link NestedTestConfiguration @NestedTestConfiguration} for details.
4242
*
43+
* <h3>Switching to default {@code TestExecutionListener} implementations</h3>
44+
*
45+
* <p>If you extend a class that is annotated with {@code @TestExecutionListeners}
46+
* and you need to switch to using the <em>default</em> set of listeners, you
47+
* can annotate your class with the following.
48+
*
49+
* <pre class="code">
50+
* // Switch to default listeners
51+
* &#064;TestExecutionListeners(listeners = {}, inheritListeners = false, mergeMode = MERGE_WITH_DEFAULTS)
52+
* class MyTests extends BaseTests {
53+
* // ...
54+
* }
55+
* </pre>
56+
*
4357
* @author Sam Brannen
4458
* @since 2.5
4559
* @see TestExecutionListener

src/docs/asciidoc/testing.adoc

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,10 +1098,10 @@ javadoc.
10981098
[[spring-testing-annotation-testexecutionlisteners]]
10991099
===== `@TestExecutionListeners`
11001100

1101-
`@TestExecutionListeners` defines class-level metadata for configuring the
1102-
`TestExecutionListener` implementations that should be registered with the
1103-
`TestContextManager`. Typically, `@TestExecutionListeners` is used in conjunction with
1104-
`@ContextConfiguration`.
1101+
`@TestExecutionListeners` is used to register listeners for a particular test class, its
1102+
subclasses, and its nested classes. If you wish to register a listener globally, you
1103+
should register it via the automatic discovery mechanism described in
1104+
<<testcontext-tel-config>>.
11051105

11061106
The following example shows how to register two `TestExecutionListener` implementations:
11071107

@@ -1132,7 +1132,9 @@ By default, `@TestExecutionListeners` provides support for inheriting listeners
11321132
superclasses or enclosing classes. See
11331133
<<testcontext-junit-jupiter-nested-test-configuration>> and the
11341134
{api-spring-framework}/test/context/TestExecutionListeners.html[`@TestExecutionListeners`
1135-
javadoc] for an example and further details.
1135+
javadoc] for an example and further details. If you discover that you need to switch
1136+
back to using the default `TestExecutionListener` implementations, see the note
1137+
in <<testcontext-tel-config-registering-tels>>.
11361138

11371139
[[spring-testing-annotation-recordapplicationevents]]
11381140
===== `@RecordApplicationEvents`
@@ -2460,12 +2462,46 @@ by default, exactly in the following order:
24602462
[[testcontext-tel-config-registering-tels]]
24612463
===== Registering `TestExecutionListener` Implementations
24622464

2463-
You can register `TestExecutionListener` implementations for a test class and its
2464-
subclasses by using the `@TestExecutionListeners` annotation. See
2465+
You can register `TestExecutionListener` implementations explicitly for a test class, its
2466+
subclasses, and its nested classes by using the `@TestExecutionListeners` annotation. See
24652467
<<integration-testing-annotations, annotation support>> and the javadoc for
24662468
{api-spring-framework}/test/context/TestExecutionListeners.html[`@TestExecutionListeners`]
24672469
for details and examples.
24682470

2471+
.Switching to default `TestExecutionListener` implementations
2472+
[NOTE]
2473+
====
2474+
If you extend a class that is annotated with `@TestExecutionListeners` and you need to
2475+
switch to using the default set of listeners, you can annotate your class with the
2476+
following.
2477+
2478+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
2479+
.Java
2480+
----
2481+
// Switch to default listeners
2482+
@TestExecutionListeners(
2483+
listeners = {},
2484+
inheritListeners = false,
2485+
mergeMode = MERGE_WITH_DEFAULTS)
2486+
class MyTest extends BaseTest {
2487+
// class body...
2488+
}
2489+
----
2490+
2491+
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
2492+
.Kotlin
2493+
----
2494+
// Switch to default listeners
2495+
@TestExecutionListeners(
2496+
listeners = [],
2497+
inheritListeners = false,
2498+
mergeMode = MERGE_WITH_DEFAULTS)
2499+
class MyTest : BaseTest {
2500+
// class body...
2501+
}
2502+
----
2503+
====
2504+
24692505
[[testcontext-tel-config-automatic-discovery]]
24702506
===== Automatic Discovery of Default `TestExecutionListener` Implementations
24712507

0 commit comments

Comments
 (0)