Skip to content

Commit 5302566

Browse files
committed
Introduce before/after test execution callbacks in TestExecutionListener
Issue: SPR-4365
1 parent 11ed265 commit 5302566

File tree

1 file changed

+68
-9
lines changed

1 file changed

+68
-9
lines changed

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

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
* @author Sam Brannen
5656
* @author Juergen Hoeller
5757
* @since 2.5
58+
* @see TestContextManager
5859
* @see org.springframework.test.context.support.AbstractTestExecutionListener
5960
*/
6061
public interface TestExecutionListener {
@@ -70,6 +71,7 @@ public interface TestExecutionListener {
7071
* concrete classes as necessary.
7172
* @param testContext the test context for the test; never {@code null}
7273
* @throws Exception allows any exception to propagate
74+
* @since 3.0
7375
*/
7476
default void beforeTestClass(TestContext testContext) throws Exception {
7577
/* no-op */
@@ -90,34 +92,90 @@ default void prepareTestInstance(TestContext testContext) throws Exception {
9092
}
9193

9294
/**
93-
* Pre-processes a test <em>before</em> execution of the
95+
* Pre-processes a test <em>before</em> execution of <em>before</em>
96+
* lifecycle callbacks of the underlying test framework &mdash; for example,
97+
* by setting up test fixtures.
98+
* <p>This method <strong>must</strong> be called immediately prior to
99+
* framework-specific <em>before</em> lifecycle callbacks. For historical
100+
* reasons, this method is named {@code beforeTestMethod}. Since the
101+
* introduction of {@link #beforeTestExecution}, a more suitable name for
102+
* this method might be something like {@code beforeTestSetUp} or
103+
* {@code beforeEach}; however, it is unfortunately impossible to rename
104+
* this method due to backward compatibility concerns.
105+
* <p>The default implementation is <em>empty</em>. Can be overridden by
106+
* concrete classes as necessary.
107+
* @param testContext the test context in which the test method will be
108+
* executed; never {@code null}
109+
* @throws Exception allows any exception to propagate
110+
* @see #afterTestMethod
111+
* @see #beforeTestExecution
112+
* @see #afterTestExecution
113+
*/
114+
default void beforeTestMethod(TestContext testContext) throws Exception {
115+
/* no-op */
116+
}
117+
118+
/**
119+
* Pre-processes a test <em>immediately before</em> execution of the
94120
* {@link java.lang.reflect.Method test method} in the supplied
95-
* {@link TestContext test context}, for example by setting up test
96-
* fixtures.
97-
* <p>This method should be called immediately prior to framework-specific
121+
* {@link TestContext test context} &mdash; for example, for timing
122+
* or logging purposes.
123+
* <p>This method <strong>must</strong> be called after framework-specific
98124
* <em>before</em> lifecycle callbacks.
99125
* <p>The default implementation is <em>empty</em>. Can be overridden by
100126
* concrete classes as necessary.
101127
* @param testContext the test context in which the test method will be
102128
* executed; never {@code null}
103129
* @throws Exception allows any exception to propagate
130+
* @since 5.0
131+
* @see #beforeTestMethod
132+
* @see #afterTestMethod
133+
* @see #afterTestExecution
104134
*/
105-
default void beforeTestMethod(TestContext testContext) throws Exception {
135+
default void beforeTestExecution(TestContext testContext) throws Exception {
106136
/* no-op */
107137
}
108138

109139
/**
110-
* Post-processes a test <em>after</em> execution of the
140+
* Post-processes a test <em>immediately after</em> execution of the
111141
* {@link java.lang.reflect.Method test method} in the supplied
112-
* {@link TestContext test context}, for example by tearing down test
113-
* fixtures.
114-
* <p>This method should be called immediately after framework-specific
142+
* {@link TestContext test context} &mdash; for example, for timing
143+
* or logging purposes.
144+
* <p>This method <strong>must</strong> be called before framework-specific
115145
* <em>after</em> lifecycle callbacks.
116146
* <p>The default implementation is <em>empty</em>. Can be overridden by
117147
* concrete classes as necessary.
148+
* @param testContext the test context in which the test method will be
149+
* executed; never {@code null}
150+
* @throws Exception allows any exception to propagate
151+
* @since 5.0
152+
* @see #beforeTestMethod
153+
* @see #afterTestMethod
154+
* @see #beforeTestExecution
155+
*/
156+
default void afterTestExecution(TestContext testContext) throws Exception {
157+
/* no-op */
158+
}
159+
160+
/**
161+
* Post-processes a test <em>after</em> execution of <em>after</em>
162+
* lifecycle callbacks of the underlying test framework &mdash; for example,
163+
* by tearing down test fixtures.
164+
* <p>This method <strong>must</strong> be called immediately after
165+
* framework-specific <em>after</em> lifecycle callbacks. For historical
166+
* reasons, this method is named {@code afterTestMethod}. Since the
167+
* introduction of {@link #afterTestExecution}, a more suitable name for
168+
* this method might be something like {@code afterTestTearDown} or
169+
* {@code afterEach}; however, it is unfortunately impossible to rename
170+
* this method due to backward compatibility concerns.
171+
* <p>The default implementation is <em>empty</em>. Can be overridden by
172+
* concrete classes as necessary.
118173
* @param testContext the test context in which the test method was
119174
* executed; never {@code null}
120175
* @throws Exception allows any exception to propagate
176+
* @see #beforeTestMethod
177+
* @see #beforeTestExecution
178+
* @see #afterTestExecution
121179
*/
122180
default void afterTestMethod(TestContext testContext) throws Exception {
123181
/* no-op */
@@ -134,6 +192,7 @@ default void afterTestMethod(TestContext testContext) throws Exception {
134192
* concrete classes as necessary.
135193
* @param testContext the test context for the test; never {@code null}
136194
* @throws Exception allows any exception to propagate
195+
* @since 3.0
137196
*/
138197
default void afterTestClass(TestContext testContext) throws Exception {
139198
/* no-op */

0 commit comments

Comments
 (0)