Introduce before/after test execution support in SpringJUnit4ClassRunner

Issue: SPR-4365
This commit is contained in:
Sam Brannen
2016-07-09 17:57:55 +02:00
parent 3da5fbe995
commit da89332840
6 changed files with 257 additions and 30 deletions

View File

@@ -42,8 +42,10 @@ import org.springframework.test.context.TestContextManager;
import org.springframework.test.context.junit4.rules.SpringClassRule;
import org.springframework.test.context.junit4.rules.SpringMethodRule;
import org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks;
import org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks;
import org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks;
import org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks;
import org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks;
import org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks;
import org.springframework.test.context.junit4.statements.SpringFailOnTimeout;
import org.springframework.test.context.junit4.statements.SpringRepeat;
@@ -270,6 +272,9 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner {
* Spring-specific timeouts in that the former execute in a separate
* thread while the latter simply execute in the main thread (like regular
* tests).
* @see #methodInvoker(FrameworkMethod, Object)
* @see #withBeforeTestExecutionCallbacks(FrameworkMethod, Object, Statement)
* @see #withAfterTestExecutionCallbacks(FrameworkMethod, Object, Statement)
* @see #possiblyExpectingExceptions(FrameworkMethod, Object, Statement)
* @see #withBefores(FrameworkMethod, Object, Statement)
* @see #withAfters(FrameworkMethod, Object, Statement)
@@ -293,6 +298,8 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner {
}
Statement statement = methodInvoker(frameworkMethod, testInstance);
statement = withBeforeTestExecutionCallbacks(frameworkMethod, testInstance, statement);
statement = withAfterTestExecutionCallbacks(frameworkMethod, testInstance, statement);
statement = possiblyExpectingExceptions(frameworkMethod, testInstance, statement);
statement = withBefores(frameworkMethod, testInstance, statement);
statement = withAfters(frameworkMethod, testInstance, statement);
@@ -404,6 +411,26 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner {
return TestAnnotationUtils.getTimeout(frameworkMethod.getMethod());
}
/**
* Wrap the supplied {@link Statement} with a {@code RunBeforeTestExecutionCallbacks}
* statement, thus preserving the default functionality while adding support for the
* Spring TestContext Framework.
* @see RunBeforeTestExecutionCallbacks
*/
protected Statement withBeforeTestExecutionCallbacks(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) {
return new RunBeforeTestExecutionCallbacks(statement, testInstance, frameworkMethod.getMethod(), getTestContextManager());
}
/**
* Wrap the supplied {@link Statement} with a {@code RunAfterTestExecutionCallbacks}
* statement, thus preserving the default functionality while adding support for the
* Spring TestContext Framework.
* @see RunAfterTestExecutionCallbacks
*/
protected Statement withAfterTestExecutionCallbacks(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) {
return new RunAfterTestExecutionCallbacks(statement, testInstance, frameworkMethod.getMethod(), getTestContextManager());
}
/**
* Wrap the {@link Statement} returned by the parent implementation with a
* {@code RunBeforeTestMethodCallbacks} statement, thus preserving the
@@ -414,8 +441,7 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner {
@Override
protected Statement withBefores(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) {
Statement junitBefores = super.withBefores(frameworkMethod, testInstance, statement);
return new RunBeforeTestMethodCallbacks(junitBefores, testInstance, frameworkMethod.getMethod(),
getTestContextManager());
return new RunBeforeTestMethodCallbacks(junitBefores, testInstance, frameworkMethod.getMethod(), getTestContextManager());
}
/**
@@ -428,8 +454,7 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner {
@Override
protected Statement withAfters(FrameworkMethod frameworkMethod, Object testInstance, Statement statement) {
Statement junitAfters = super.withAfters(frameworkMethod, testInstance, statement);
return new RunAfterTestMethodCallbacks(junitAfters, testInstance, frameworkMethod.getMethod(),
getTestContextManager());
return new RunAfterTestMethodCallbacks(junitAfters, testInstance, frameworkMethod.getMethod(), getTestContextManager());
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -37,7 +37,7 @@ import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
/**
* {@code SpringMethodRule} is a custom JUnit {@link MethodRule} that
* {@code SpringMethodRule} is a custom JUnit 4 {@link MethodRule} that
* supports instance-level and method-level features of the
* <em>Spring TestContext Framework</em> in standard JUnit tests by means
* of the {@link TestContextManager} and associated support classes and
@@ -82,6 +82,12 @@ import org.springframework.util.ReflectionUtils;
*
* <p><strong>NOTE:</strong> As of Spring Framework 4.3, this class requires JUnit 4.12 or higher.
*
* <p><strong>WARNING:</strong> Due to the shortcomings of JUnit rules, the
* {@code SpringMethodRule} does <strong>not</strong> support the
* {@code beforeTestExecution()} and {@code afterTestExecution()} callbacks of the
* {@link org.springframework.test.context.TestExecutionListener TestExecutionListener}
* API.
*
* @author Sam Brannen
* @author Philippe Marschall
* @since 4.2

View File

@@ -0,0 +1,100 @@
/*
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.junit4.statements;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.junit.runners.model.MultipleFailureException;
import org.junit.runners.model.Statement;
import org.springframework.test.context.TestContextManager;
/**
* {@code RunAfterTestExecutionCallbacks} is a custom JUnit {@link Statement}
* which allows the <em>Spring TestContext Framework</em> to be plugged into the
* JUnit 4 execution chain by calling {@link TestContextManager#afterTestExecution
* afterTestExecution()} on the supplied {@link TestContextManager}.
*
* <p><strong>NOTE:</strong> This class requires JUnit 4.9 or higher.
*
* @author Sam Brannen
* @since 5.0
* @see #evaluate()
* @see RunBeforeTestExecutionCallbacks
*/
public class RunAfterTestExecutionCallbacks extends Statement {
private final Statement next;
private final Object testInstance;
private final Method testMethod;
private final TestContextManager testContextManager;
/**
* Construct a new {@code RunAfterTestExecutionCallbacks} statement.
* @param next the next {@code Statement} in the execution chain
* @param testInstance the current test instance (never {@code null})
* @param testMethod the test method which has just been executed on the
* test instance
* @param testContextManager the TestContextManager upon which to call
* {@code afterTestExecution()}
*/
public RunAfterTestExecutionCallbacks(Statement next, Object testInstance, Method testMethod,
TestContextManager testContextManager) {
this.next = next;
this.testInstance = testInstance;
this.testMethod = testMethod;
this.testContextManager = testContextManager;
}
/**
* Evaluate the next {@link Statement} in the execution chain (typically an
* instance of {@link RunBeforeTestExecutionCallbacks}), catching any exceptions
* thrown, and then invoke {@link TestContextManager#afterTestExecution} supplying
* the first caught exception (if any).
* <p>If the invocation of {@code afterTestExecution()} throws an exception, that
* exception will also be tracked. Multiple exceptions will be combined into a
* {@link MultipleFailureException}.
*/
@Override
public void evaluate() throws Throwable {
Throwable testException = null;
List<Throwable> errors = new ArrayList<>();
try {
this.next.evaluate();
}
catch (Throwable ex) {
testException = ex;
errors.add(ex);
}
try {
this.testContextManager.afterTestExecution(this.testInstance, this.testMethod, testException);
}
catch (Throwable ex) {
errors.add(ex);
}
MultipleFailureException.assertEmpty(errors);
}
}

View File

@@ -0,0 +1,76 @@
/*
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.junit4.statements;
import java.lang.reflect.Method;
import org.junit.runners.model.Statement;
import org.springframework.test.context.TestContextManager;
/**
* {@code RunBeforeTestExecutionCallbacks} is a custom JUnit {@link Statement}
* which allows the <em>Spring TestContext Framework</em> to be plugged into the
* JUnit 4 execution chain by calling {@link TestContextManager#beforeTestExecution
* beforeTestExecution()} on the supplied {@link TestContextManager}.
*
* @author Sam Brannen
* @since 5.0
* @see #evaluate()
* @see RunAfterTestExecutionCallbacks
*/
public class RunBeforeTestExecutionCallbacks extends Statement {
private final Statement next;
private final Object testInstance;
private final Method testMethod;
private final TestContextManager testContextManager;
/**
* Construct a new {@code RunBeforeTestExecutionCallbacks} statement.
* @param next the next {@code Statement} in the execution chain
* @param testInstance the current test instance (never {@code null})
* @param testMethod the test method which is about to be executed on the
* test instance
* @param testContextManager the TestContextManager upon which to call
* {@code beforeTestExecution()}
*/
public RunBeforeTestExecutionCallbacks(Statement next, Object testInstance, Method testMethod,
TestContextManager testContextManager) {
this.next = next;
this.testInstance = testInstance;
this.testMethod = testMethod;
this.testContextManager = testContextManager;
}
/**
* Invoke {@link TestContextManager#beforeTestExecution(Object, Method)}
* and then evaluate the next {@link Statement} in the execution chain
* (typically an instance of
* {@link org.junit.internal.runners.statements.InvokeMethod InvokeMethod}).
*/
@Override
public void evaluate() throws Throwable {
this.testContextManager.beforeTestExecution(this.testInstance, this.testMethod);
this.next.evaluate();
}
}