Skip to content

Commit da89332

Browse files
committed
Introduce before/after test execution support in SpringJUnit4ClassRunner
Issue: SPR-4365
1 parent 3da5fbe commit da89332

File tree

6 files changed

+257
-30
lines changed

6 files changed

+257
-30
lines changed

spring-test/src/main/java/org/springframework/test/context/junit4/SpringJUnit4ClassRunner.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@
4242
import org.springframework.test.context.junit4.rules.SpringClassRule;
4343
import org.springframework.test.context.junit4.rules.SpringMethodRule;
4444
import org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks;
45+
import org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks;
4546
import org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks;
4647
import org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks;
48+
import org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks;
4749
import org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks;
4850
import org.springframework.test.context.junit4.statements.SpringFailOnTimeout;
4951
import org.springframework.test.context.junit4.statements.SpringRepeat;
@@ -270,6 +272,9 @@ protected void runChild(FrameworkMethod frameworkMethod, RunNotifier notifier) {
270272
* Spring-specific timeouts in that the former execute in a separate
271273
* thread while the latter simply execute in the main thread (like regular
272274
* tests).
275+
* @see #methodInvoker(FrameworkMethod, Object)
276+
* @see #withBeforeTestExecutionCallbacks(FrameworkMethod, Object, Statement)
277+
* @see #withAfterTestExecutionCallbacks(FrameworkMethod, Object, Statement)
273278
* @see #possiblyExpectingExceptions(FrameworkMethod, Object, Statement)
274279
* @see #withBefores(FrameworkMethod, Object, Statement)
275280
* @see #withAfters(FrameworkMethod, Object, Statement)
@@ -293,6 +298,8 @@ protected Object runReflectiveCall() throws Throwable {
293298
}
294299

295300
Statement statement = methodInvoker(frameworkMethod, testInstance);
301+
statement = withBeforeTestExecutionCallbacks(frameworkMethod, testInstance, statement);
302+
statement = withAfterTestExecutionCallbacks(frameworkMethod, testInstance, statement);
296303
statement = possiblyExpectingExceptions(frameworkMethod, testInstance, statement);
297304
statement = withBefores(frameworkMethod, testInstance, statement);
298305
statement = withAfters(frameworkMethod, testInstance, statement);
@@ -404,6 +411,26 @@ protected long getSpringTimeout(FrameworkMethod frameworkMethod) {
404411
return TestAnnotationUtils.getTimeout(frameworkMethod.getMethod());
405412
}
406413

414+
/**
415+
* Wrap the supplied {@link Statement} with a {@code RunBeforeTestExecutionCallbacks}
416+
* statement, thus preserving the default functionality while adding support for the
417+
* Spring TestContext Framework.
418+
* @see RunBeforeTestExecutionCallbacks
419+
*/
420+
protected Statement withBeforeTestExecutionCallbacks(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) {
421+
return new RunBeforeTestExecutionCallbacks(statement, testInstance, frameworkMethod.getMethod(), getTestContextManager());
422+
}
423+
424+
/**
425+
* Wrap the supplied {@link Statement} with a {@code RunAfterTestExecutionCallbacks}
426+
* statement, thus preserving the default functionality while adding support for the
427+
* Spring TestContext Framework.
428+
* @see RunAfterTestExecutionCallbacks
429+
*/
430+
protected Statement withAfterTestExecutionCallbacks(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) {
431+
return new RunAfterTestExecutionCallbacks(statement, testInstance, frameworkMethod.getMethod(), getTestContextManager());
432+
}
433+
407434
/**
408435
* Wrap the {@link Statement} returned by the parent implementation with a
409436
* {@code RunBeforeTestMethodCallbacks} statement, thus preserving the
@@ -414,8 +441,7 @@ protected long getSpringTimeout(FrameworkMethod frameworkMethod) {
414441
@Override
415442
protected Statement withBefores(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) {
416443
Statement junitBefores = super.withBefores(frameworkMethod, testInstance, statement);
417-
return new RunBeforeTestMethodCallbacks(junitBefores, testInstance, frameworkMethod.getMethod(),
418-
getTestContextManager());
444+
return new RunBeforeTestMethodCallbacks(junitBefores, testInstance, frameworkMethod.getMethod(), getTestContextManager());
419445
}
420446

421447
/**
@@ -428,8 +454,7 @@ protected Statement withBefores(FrameworkMethod frameworkMethod, Object testInst
428454
@Override
429455
protected Statement withAfters(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) {
430456
Statement junitAfters = super.withAfters(frameworkMethod, testInstance, statement);
431-
return new RunAfterTestMethodCallbacks(junitAfters, testInstance, frameworkMethod.getMethod(),
432-
getTestContextManager());
457+
return new RunAfterTestMethodCallbacks(junitAfters, testInstance, frameworkMethod.getMethod(), getTestContextManager());
433458
}
434459

435460
/**

spring-test/src/main/java/org/springframework/test/context/junit4/rules/SpringMethodRule.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -37,7 +37,7 @@
3737
import org.springframework.util.ReflectionUtils;
3838

3939
/**
40-
* {@code SpringMethodRule} is a custom JUnit {@link MethodRule} that
40+
* {@code SpringMethodRule} is a custom JUnit 4 {@link MethodRule} that
4141
* supports instance-level and method-level features of the
4242
* <em>Spring TestContext Framework</em> in standard JUnit tests by means
4343
* of the {@link TestContextManager} and associated support classes and
@@ -82,6 +82,12 @@
8282
*
8383
* <p><strong>NOTE:</strong> As of Spring Framework 4.3, this class requires JUnit 4.12 or higher.
8484
*
85+
* <p><strong>WARNING:</strong> Due to the shortcomings of JUnit rules, the
86+
* {@code SpringMethodRule} does <strong>not</strong> support the
87+
* {@code beforeTestExecution()} and {@code afterTestExecution()} callbacks of the
88+
* {@link org.springframework.test.context.TestExecutionListener TestExecutionListener}
89+
* API.
90+
*
8591
* @author Sam Brannen
8692
* @author Philippe Marschall
8793
* @since 4.2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.junit4.statements;
18+
19+
import java.lang.reflect.Method;
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
23+
import org.junit.runners.model.MultipleFailureException;
24+
import org.junit.runners.model.Statement;
25+
import org.springframework.test.context.TestContextManager;
26+
27+
/**
28+
* {@code RunAfterTestExecutionCallbacks} is a custom JUnit {@link Statement}
29+
* which allows the <em>Spring TestContext Framework</em> to be plugged into the
30+
* JUnit 4 execution chain by calling {@link TestContextManager#afterTestExecution
31+
* afterTestExecution()} on the supplied {@link TestContextManager}.
32+
*
33+
* <p><strong>NOTE:</strong> This class requires JUnit 4.9 or higher.
34+
*
35+
* @author Sam Brannen
36+
* @since 5.0
37+
* @see #evaluate()
38+
* @see RunBeforeTestExecutionCallbacks
39+
*/
40+
public class RunAfterTestExecutionCallbacks extends Statement {
41+
42+
private final Statement next;
43+
44+
private final Object testInstance;
45+
46+
private final Method testMethod;
47+
48+
private final TestContextManager testContextManager;
49+
50+
51+
/**
52+
* Construct a new {@code RunAfterTestExecutionCallbacks} statement.
53+
* @param next the next {@code Statement} in the execution chain
54+
* @param testInstance the current test instance (never {@code null})
55+
* @param testMethod the test method which has just been executed on the
56+
* test instance
57+
* @param testContextManager the TestContextManager upon which to call
58+
* {@code afterTestExecution()}
59+
*/
60+
public RunAfterTestExecutionCallbacks(Statement next, Object testInstance, Method testMethod,
61+
TestContextManager testContextManager) {
62+
63+
this.next = next;
64+
this.testInstance = testInstance;
65+
this.testMethod = testMethod;
66+
this.testContextManager = testContextManager;
67+
}
68+
69+
/**
70+
* Evaluate the next {@link Statement} in the execution chain (typically an
71+
* instance of {@link RunBeforeTestExecutionCallbacks}), catching any exceptions
72+
* thrown, and then invoke {@link TestContextManager#afterTestExecution} supplying
73+
* the first caught exception (if any).
74+
* <p>If the invocation of {@code afterTestExecution()} throws an exception, that
75+
* exception will also be tracked. Multiple exceptions will be combined into a
76+
* {@link MultipleFailureException}.
77+
*/
78+
@Override
79+
public void evaluate() throws Throwable {
80+
Throwable testException = null;
81+
List<Throwable> errors = new ArrayList<>();
82+
try {
83+
this.next.evaluate();
84+
}
85+
catch (Throwable ex) {
86+
testException = ex;
87+
errors.add(ex);
88+
}
89+
90+
try {
91+
this.testContextManager.afterTestExecution(this.testInstance, this.testMethod, testException);
92+
}
93+
catch (Throwable ex) {
94+
errors.add(ex);
95+
}
96+
97+
MultipleFailureException.assertEmpty(errors);
98+
}
99+
100+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.junit4.statements;
18+
19+
import java.lang.reflect.Method;
20+
21+
import org.junit.runners.model.Statement;
22+
import org.springframework.test.context.TestContextManager;
23+
24+
/**
25+
* {@code RunBeforeTestExecutionCallbacks} is a custom JUnit {@link Statement}
26+
* which allows the <em>Spring TestContext Framework</em> to be plugged into the
27+
* JUnit 4 execution chain by calling {@link TestContextManager#beforeTestExecution
28+
* beforeTestExecution()} on the supplied {@link TestContextManager}.
29+
*
30+
* @author Sam Brannen
31+
* @since 5.0
32+
* @see #evaluate()
33+
* @see RunAfterTestExecutionCallbacks
34+
*/
35+
public class RunBeforeTestExecutionCallbacks extends Statement {
36+
37+
private final Statement next;
38+
39+
private final Object testInstance;
40+
41+
private final Method testMethod;
42+
43+
private final TestContextManager testContextManager;
44+
45+
46+
/**
47+
* Construct a new {@code RunBeforeTestExecutionCallbacks} statement.
48+
* @param next the next {@code Statement} in the execution chain
49+
* @param testInstance the current test instance (never {@code null})
50+
* @param testMethod the test method which is about to be executed on the
51+
* test instance
52+
* @param testContextManager the TestContextManager upon which to call
53+
* {@code beforeTestExecution()}
54+
*/
55+
public RunBeforeTestExecutionCallbacks(Statement next, Object testInstance, Method testMethod,
56+
TestContextManager testContextManager) {
57+
58+
this.next = next;
59+
this.testInstance = testInstance;
60+
this.testMethod = testMethod;
61+
this.testContextManager = testContextManager;
62+
}
63+
64+
/**
65+
* Invoke {@link TestContextManager#beforeTestExecution(Object, Method)}
66+
* and then evaluate the next {@link Statement} in the execution chain
67+
* (typically an instance of
68+
* {@link org.junit.internal.runners.statements.InvokeMethod InvokeMethod}).
69+
*/
70+
@Override
71+
public void evaluate() throws Throwable {
72+
this.testContextManager.beforeTestExecution(this.testInstance, this.testMethod);
73+
this.next.evaluate();
74+
}
75+
76+
}

0 commit comments

Comments
 (0)