Skip to content

Commit 52e01bb

Browse files
committed
Merge branch '6.2.x'
2 parents cd4e73a + c5ecc50 commit 52e01bb

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

framework-docs/modules/ROOT/pages/testing/testcontext-framework/tel-config.adoc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ by default, exactly in the following order:
77
* `ServletTestExecutionListener`: Configures Servlet API mocks for a
88
`WebApplicationContext`.
99
* `DirtiesContextBeforeModesTestExecutionListener`: Handles the `@DirtiesContext`
10-
annotation for "`before`" modes.
10+
annotation for "before" modes.
1111
* `ApplicationEventsTestExecutionListener`: Provides support for
1212
xref:testing/testcontext-framework/application-events.adoc[`ApplicationEvents`].
13-
* `BeanOverrideTestExecutionListener`: Provides support for xref:testing/testcontext-framework/bean-overriding.adoc[] .
13+
* `BeanOverrideTestExecutionListener`: Provides support for
14+
xref:testing/testcontext-framework/bean-overriding.adoc[].
1415
* `DependencyInjectionTestExecutionListener`: Provides dependency injection for the test
1516
instance.
1617
* `MicrometerObservationRegistryTestExecutionListener`: Provides support for
1718
Micrometer's `ObservationRegistry`.
1819
* `DirtiesContextTestExecutionListener`: Handles the `@DirtiesContext` annotation for
19-
"`after`" modes.
20+
"after" modes.
2021
* `CommonCachesTestExecutionListener`: Clears resource caches in the test's
2122
`ApplicationContext` if necessary.
2223
* `TransactionalTestExecutionListener`: Provides transactional test execution with
@@ -161,15 +162,16 @@ change from release to release -- for example, `SqlScriptsTestExecutionListener`
161162
introduced in Spring Framework 4.1, and `DirtiesContextBeforeModesTestExecutionListener`
162163
was introduced in Spring Framework 4.2. Furthermore, third-party frameworks like Spring
163164
Boot and Spring Security register their own default `TestExecutionListener`
164-
implementations by using the aforementioned xref:testing/testcontext-framework/tel-config.adoc#testcontext-tel-config-automatic-discovery[automatic discovery mechanism]
165-
.
165+
implementations by using the aforementioned
166+
xref:testing/testcontext-framework/tel-config.adoc#testcontext-tel-config-automatic-discovery[automatic discovery mechanism].
166167

167168
To avoid having to be aware of and re-declare all default listeners, you can set the
168169
`mergeMode` attribute of `@TestExecutionListeners` to `MergeMode.MERGE_WITH_DEFAULTS`.
169170
`MERGE_WITH_DEFAULTS` indicates that locally declared listeners should be merged with the
170171
default listeners. The merging algorithm ensures that duplicates are removed from the
171172
list and that the resulting set of merged listeners is sorted according to the semantics
172-
of `AnnotationAwareOrderComparator`, as described in xref:testing/testcontext-framework/tel-config.adoc#testcontext-tel-config-ordering[Ordering `TestExecutionListener` Implementations].
173+
of `AnnotationAwareOrderComparator`, as described in
174+
xref:testing/testcontext-framework/tel-config.adoc#testcontext-tel-config-ordering[Ordering `TestExecutionListener` Implementations].
173175
If a listener implements `Ordered` or is annotated with `@Order`, it can influence the
174176
position in which it is merged with the defaults. Otherwise, locally declared listeners
175177
are appended to the list of default listeners when merged.

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

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -22,8 +22,9 @@
2222
* the listener is registered.
2323
*
2424
* <p>Note that not all testing frameworks support all lifecycle callbacks defined
25-
* in this API. For example, {@link #beforeTestExecution} and
26-
* {@link #afterTestExecution} are not supported in conjunction with JUnit 4 when
25+
* in this API. For example, the {@link #beforeTestExecution(TestContext)
26+
* beforeTestExecution} and {@link #afterTestExecution(TestContext)
27+
* afterTestExecution} callbacks are not supported in conjunction with JUnit 4 when
2728
* using the {@link org.springframework.test.context.junit4.rules.SpringMethodRule
2829
* SpringMethodRule}.
2930
*
@@ -41,6 +42,35 @@
4142
* {@link org.springframework.core.annotation.Order @Order} annotation. See
4243
* {@link TestContextBootstrapper#getTestExecutionListeners()} for details.
4344
*
45+
* <h3>Wrapping Behavior for Listeners</h3>
46+
*
47+
* <p>The {@link TestContextManager} guarantees <em>wrapping</em> behavior for
48+
* multiple registered listeners that implement lifecycle callbacks such as
49+
* {@link #beforeTestClass(TestContext) beforeTestClass},
50+
* {@link #afterTestClass(TestContext) afterTestClass},
51+
* {@link #beforeTestMethod(TestContext) beforeTestMethod},
52+
* {@link #afterTestMethod(TestContext) afterTestMethod},
53+
* {@link #beforeTestExecution(TestContext) beforeTestExecution}, and
54+
* {@link #afterTestExecution(TestContext) afterTestExecution}. This means that,
55+
* given two listeners {@code Listener1} and {@code Listener2} with {@code Listener1}
56+
* registered before {@code Listener2}, any <em>before</em> callbacks implemented
57+
* by {@code Listener1} are guaranteed to be invoked <strong>before</strong> any
58+
* <em>before</em> callbacks implemented by {@code Listener2}. Similarly, given
59+
* the same two listeners registered in the same order, any <em>after</em>
60+
* callbacks implemented by {@code Listener1} are guaranteed to be invoked
61+
* <strong>after</strong> any <em>after</em> callbacks implemented by
62+
* {@code Listener2}. {@code Listener1} is therefore said to <em>wrap</em>
63+
* {@code Listener2}.
64+
*
65+
* <p>For a concrete example, consider the relationship between the
66+
* {@link org.springframework.test.context.transaction.TransactionalTestExecutionListener
67+
* TransactionalTestExecutionListener} and the
68+
* {@link org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener
69+
* SqlScriptsTestExecutionListener}. The {@code SqlScriptsTestExecutionListener}
70+
* is registered after the {@code TransactionalTestExecutionListener}, so that
71+
* SQL scripts are executed within a transaction managed by the
72+
* {@code TransactionalTestExecutionListener}.
73+
*
4474
* <h3>Registering TestExecutionListener Implementations</h3>
4575
*
4676
* <p>A {@code TestExecutionListener} can be registered explicitly for a test class,
@@ -100,6 +130,8 @@ public interface TestExecutionListener {
100130
* the class.
101131
* <p>This method should be called immediately before framework-specific
102132
* <em>before class</em> lifecycle callbacks.
133+
* <p>See the {@linkplain TestExecutionListener class-level documentation}
134+
* for details on wrapping behavior for lifecycle callbacks.
103135
* <p>The default implementation is <em>empty</em>. Can be overridden by
104136
* concrete classes as necessary.
105137
* @param testContext the test context for the test; never {@code null}
@@ -118,6 +150,8 @@ default void beforeTestClass(TestContext testContext) throws Exception {
118150
* {@link org.springframework.test.context.junit4.rules.SpringMethodRule
119151
* SpringMethodRule}). In any case, this method must be called prior to any
120152
* framework-specific lifecycle callbacks.
153+
* <p>See the {@linkplain TestExecutionListener class-level documentation}
154+
* for details on wrapping behavior for listeners.
121155
* <p>The default implementation is <em>empty</em>. Can be overridden by
122156
* concrete classes as necessary.
123157
* @param testContext the test context for the test; never {@code null}
@@ -137,6 +171,8 @@ default void prepareTestInstance(TestContext testContext) throws Exception {
137171
* this method might be something like {@code beforeTestSetUp} or
138172
* {@code beforeEach}; however, it is unfortunately impossible to rename
139173
* this method due to backward compatibility concerns.
174+
* <p>See the {@linkplain TestExecutionListener class-level documentation}
175+
* for details on wrapping behavior for lifecycle callbacks.
140176
* <p>The default implementation is <em>empty</em>. Can be overridden by
141177
* concrete classes as necessary.
142178
* @param testContext the test context in which the test method will be
@@ -156,6 +192,8 @@ default void beforeTestMethod(TestContext testContext) throws Exception {
156192
* or logging purposes.
157193
* <p>This method <strong>must</strong> be called after framework-specific
158194
* <em>before</em> lifecycle callbacks.
195+
* <p>See the {@linkplain TestExecutionListener class-level documentation}
196+
* for details on wrapping behavior for lifecycle callbacks.
159197
* <p>The default implementation is <em>empty</em>. Can be overridden by
160198
* concrete classes as necessary.
161199
* @param testContext the test context in which the test method will be
@@ -176,6 +214,8 @@ default void beforeTestExecution(TestContext testContext) throws Exception {
176214
* or logging purposes.
177215
* <p>This method <strong>must</strong> be called before framework-specific
178216
* <em>after</em> lifecycle callbacks.
217+
* <p>See the {@linkplain TestExecutionListener class-level documentation}
218+
* for details on wrapping behavior for lifecycle callbacks.
179219
* <p>The default implementation is <em>empty</em>. Can be overridden by
180220
* concrete classes as necessary.
181221
* @param testContext the test context in which the test method will be
@@ -200,6 +240,8 @@ default void afterTestExecution(TestContext testContext) throws Exception {
200240
* this method might be something like {@code afterTestTearDown} or
201241
* {@code afterEach}; however, it is unfortunately impossible to rename
202242
* this method due to backward compatibility concerns.
243+
* <p>See the {@linkplain TestExecutionListener class-level documentation}
244+
* for details on wrapping behavior for lifecycle callbacks.
203245
* <p>The default implementation is <em>empty</em>. Can be overridden by
204246
* concrete classes as necessary.
205247
* @param testContext the test context in which the test method was
@@ -217,6 +259,8 @@ default void afterTestMethod(TestContext testContext) throws Exception {
217259
* the class.
218260
* <p>This method should be called immediately after framework-specific
219261
* <em>after class</em> lifecycle callbacks.
262+
* <p>See the {@linkplain TestExecutionListener class-level documentation}
263+
* for details on wrapping behavior for lifecycle callbacks.
220264
* <p>The default implementation is <em>empty</em>. Can be overridden by
221265
* concrete classes as necessary.
222266
* @param testContext the test context for the test; never {@code null}

0 commit comments

Comments
 (0)