Browse Source

Introduce before/after test execution callbacks in the TCF

This commit introduces 'before' and 'after' test execution callbacks in
the Spring TestContext Framework. Specifically, this set of commits
introduces the following.

- beforeTestExecution() and afterTestExecution() callbacks in the
  TestExecutionListener API

- beforeTestExecution() and afterTestExecution() callbacks in
  TestContextManager

- RunBeforeTestExecutionCallbacks and RunAfterTestExecutionCallbacks
  JUnit 4 statements that are used by the SpringJUnit4ClassRunner

- Documentation in the class-level Javadoc for SpringMethodRule stating
  that before/after test execution callbacks cannot be supported with
  JUnit 4 Rules

- Support for JUnit Jupiter's BeforeTestExecutionCallback and
  AfterTestExecutionCallback extension APIs in the SpringExtension for
  JUnit 5

- Support for before/after test execution callbacks in
  AbstractTestNGSpringContextTests for TestNG

Issue: SPR-4365
pull/1106/head
Sam Brannen 10 years ago
parent
commit
f7f37cdb1b
  1. 258
      spring-test/src/main/java/org/springframework/test/context/TestContextManager.java
  2. 77
      spring-test/src/main/java/org/springframework/test/context/TestExecutionListener.java
  3. 26
      spring-test/src/main/java/org/springframework/test/context/junit/jupiter/SpringExtension.java
  4. 33
      spring-test/src/main/java/org/springframework/test/context/junit4/SpringJUnit4ClassRunner.java
  5. 10
      spring-test/src/main/java/org/springframework/test/context/junit4/rules/SpringMethodRule.java
  6. 100
      spring-test/src/main/java/org/springframework/test/context/junit4/statements/RunAfterTestExecutionCallbacks.java
  7. 76
      spring-test/src/main/java/org/springframework/test/context/junit4/statements/RunBeforeTestExecutionCallbacks.java
  8. 69
      spring-test/src/main/java/org/springframework/test/context/testng/AbstractTestNGSpringContextTests.java
  9. 174
      spring-test/src/test/java/org/springframework/test/context/TestContextManagerTests.java
  10. 264
      spring-test/src/test/java/org/springframework/test/context/junit/jupiter/FailingBeforeAndAfterMethodsSpringExtensionTestCase.java
  11. 61
      spring-test/src/test/java/org/springframework/test/context/junit4/FailingBeforeAndAfterMethodsSpringRunnerTests.java
  12. 107
      spring-test/src/test/java/org/springframework/test/context/junit4/FailingBeforeAndAfterMethodsTestNGTests.java
  13. 7
      spring-test/src/test/java/org/springframework/test/context/junit4/rules/FailingBeforeAndAfterMethodsSpringRuleTests.java

258
spring-test/src/main/java/org/springframework/test/context/TestContextManager.java

@ -40,23 +40,29 @@ import org.springframework.util.ReflectionUtils;
* <li>{@link #beforeTestClass() before test class execution}: prior to any * <li>{@link #beforeTestClass() before test class execution}: prior to any
* <em>before class callbacks</em> of a particular testing framework (e.g., * <em>before class callbacks</em> of a particular testing framework (e.g.,
* JUnit 4's {@link org.junit.BeforeClass @BeforeClass})</li> * JUnit 4's {@link org.junit.BeforeClass @BeforeClass})</li>
* <li>{@link #prepareTestInstance(Object) test instance preparation}: * <li>{@link #prepareTestInstance test instance preparation}:
* immediately following instantiation of the test instance</li> * immediately following instantiation of the test class</li>
* <li>{@link #beforeTestMethod(Object, Method) before test method execution}: * <li>{@link #beforeTestMethod before test setup}:
* prior to any <em>before method callbacks</em> of a particular testing framework * prior to any <em>before method callbacks</em> of a particular testing framework
* (e.g., JUnit 4's {@link org.junit.Before @Before})</li> * (e.g., JUnit 4's {@link org.junit.Before @Before})</li>
* <li>{@link #afterTestMethod(Object, Method, Throwable) after test method * <li>{@link #beforeTestExecution before test execution}:
* execution}: after any <em>after method callbacks</em> of a particular testing * immediately before execution of the {@linkplain java.lang.reflect.Method
* test method} but after test setup</li>
* <li>{@link #afterTestExecution after test execution}:
* immediately after execution of the {@linkplain java.lang.reflect.Method
* test method} but before test tear down</li>
* <li>{@link #afterTestMethod(Object, Method, Throwable) after test tear down}:
* after any <em>after method callbacks</em> of a particular testing
* framework (e.g., JUnit 4's {@link org.junit.After @After})</li> * framework (e.g., JUnit 4's {@link org.junit.After @After})</li>
* <li>{@link #afterTestClass() after test class execution}: after any * <li>{@link #afterTestClass() after test class execution}: after any
* <em>after class callbacks</em> of a particular testing framework (e.g., JUnit * <em>after class callbacks</em> of a particular testing framework (e.g., JUnit 4's
* 4's {@link org.junit.AfterClass @AfterClass})</li> * {@link org.junit.AfterClass @AfterClass})</li>
* </ul> * </ul>
* *
* <p>Support for loading and accessing * <p>Support for loading and accessing
* {@link org.springframework.context.ApplicationContext application contexts}, * {@linkplain org.springframework.context.ApplicationContext application contexts},
* dependency injection of test instances, * dependency injection of test instances,
* {@link org.springframework.transaction.annotation.Transactional transactional} * {@linkplain org.springframework.transaction.annotation.Transactional transactional}
* execution of test methods, etc. is provided by * execution of test methods, etc. is provided by
* {@link SmartContextLoader ContextLoaders} and {@link TestExecutionListener * {@link SmartContextLoader ContextLoaders} and {@link TestExecutionListener
* TestExecutionListeners}, which are configured via * TestExecutionListeners}, which are configured via
@ -173,7 +179,7 @@ public class TestContextManager {
/** /**
* Hook for pre-processing a test class <em>before</em> execution of any * Hook for pre-processing a test class <em>before</em> execution of any
* tests within the class. Should be called prior to any framework-specific * tests within the class. Should be called prior to any framework-specific
* <em>before class methods</em> (e.g., methods annotated with JUnit's * <em>before class methods</em> (e.g., methods annotated with JUnit 4's
* {@link org.junit.BeforeClass @BeforeClass}). * {@link org.junit.BeforeClass @BeforeClass}).
* <p>An attempt will be made to give each registered * <p>An attempt will be made to give each registered
* {@link TestExecutionListener} a chance to pre-process the test class * {@link TestExecutionListener} a chance to pre-process the test class
@ -181,6 +187,7 @@ public class TestContextManager {
* registered listeners will <strong>not</strong> be called. * registered listeners will <strong>not</strong> be called.
* @throws Exception if a registered TestExecutionListener throws an * @throws Exception if a registered TestExecutionListener throws an
* exception * exception
* @since 3.0
* @see #getTestExecutionListeners() * @see #getTestExecutionListeners()
*/ */
public void beforeTestClass() throws Exception { public void beforeTestClass() throws Exception {
@ -195,10 +202,7 @@ public class TestContextManager {
testExecutionListener.beforeTestClass(getTestContext()); testExecutionListener.beforeTestClass(getTestContext());
} }
catch (Throwable ex) { catch (Throwable ex) {
if (logger.isWarnEnabled()) { logException(ex, "beforeTestClass", testExecutionListener, testClass);
logger.warn("Caught exception while allowing TestExecutionListener [" + testExecutionListener +
"] to process 'before class' callback for test class [" + testClass + "]", ex);
}
ReflectionUtils.rethrowException(ex); ReflectionUtils.rethrowException(ex);
} }
} }
@ -240,60 +244,101 @@ public class TestContextManager {
} }
/** /**
* Hook for pre-processing a test <em>before</em> execution of the supplied * Hook for pre-processing a test <em>before</em> execution of <em>before</em>
* {@link Method test method}, for example for setting up test fixtures, * lifecycle callbacks of the underlying test framework &mdash; for example,
* starting a transaction, etc. Should be called prior to any * setting up test fixtures, starting a transaction, etc.
* framework-specific <em>before methods</em> (e.g., methods annotated with * <p>This method <strong>must</strong> be called immediately prior to
* JUnit's {@link org.junit.Before @Before}). * framework-specific <em>before</em> lifecycle callbacks (e.g., methods
* annotated with JUnit 4's {@link org.junit.Before @Before}). For historical
* reasons, this method is named {@code beforeTestMethod}. Since the
* introduction of {@link #beforeTestExecution}, a more suitable name for
* this method might be something like {@code beforeTestSetUp} or
* {@code beforeEach}; however, it is unfortunately impossible to rename
* this method due to backward compatibility concerns.
* <p>The managed {@link TestContext} will be updated with the supplied * <p>The managed {@link TestContext} will be updated with the supplied
* {@code testInstance} and {@code testMethod}. * {@code testInstance} and {@code testMethod}.
* <p>An attempt will be made to give each registered * <p>An attempt will be made to give each registered
* {@link TestExecutionListener} a chance to pre-process the test method * {@link TestExecutionListener} a chance to perform its pre-processing.
* execution. If a listener throws an exception, however, the remaining * If a listener throws an exception, however, the remaining registered
* registered listeners will <strong>not</strong> be called. * listeners will <strong>not</strong> be called.
* @param testInstance the current test instance (never {@code null}) * @param testInstance the current test instance (never {@code null})
* @param testMethod the test method which is about to be executed on the * @param testMethod the test method which is about to be executed on the
* test instance * test instance
* @throws Exception if a registered TestExecutionListener throws an exception * @throws Exception if a registered TestExecutionListener throws an exception
* @see #afterTestMethod
* @see #beforeTestExecution
* @see #afterTestExecution
* @see #getTestExecutionListeners() * @see #getTestExecutionListeners()
*/ */
public void beforeTestMethod(Object testInstance, Method testMethod) throws Exception { public void beforeTestMethod(Object testInstance, Method testMethod) throws Exception {
Assert.notNull(testInstance, "Test instance must not be null"); String callbackName = "beforeTestMethod";
if (logger.isTraceEnabled()) { prepareForBeforeCallback(callbackName, testInstance, testMethod);
logger.trace("beforeTestMethod(): instance [" + testInstance + "], method [" + testMethod + "]");
}
getTestContext().updateState(testInstance, testMethod, null);
for (TestExecutionListener testExecutionListener : getTestExecutionListeners()) { for (TestExecutionListener testExecutionListener : getTestExecutionListeners()) {
try { try {
testExecutionListener.beforeTestMethod(getTestContext()); testExecutionListener.beforeTestMethod(getTestContext());
} }
catch (Throwable ex) { catch (Throwable ex) {
if (logger.isWarnEnabled()) { handleBeforeException(ex, callbackName, testExecutionListener, testInstance, testMethod);
logger.warn("Caught exception while allowing TestExecutionListener [" + testExecutionListener + }
"] to process 'before' execution of test method [" + testMethod + "] for test instance [" + }
testInstance + "]", ex); }
}
ReflectionUtils.rethrowException(ex); /**
* Hook for pre-processing a test <em>immediately before</em> execution of
* the {@linkplain java.lang.reflect.Method test method} in the supplied
* {@linkplain TestContext test context} &mdash; for example, for timing
* or logging purposes.
* <p>This method <strong>must</strong> be called after framework-specific
* <em>before</em> lifecycle callbacks (e.g., methods annotated with JUnit 4's
* {@link org.junit.Before @Before}).
* <p>The managed {@link TestContext} will be updated with the supplied
* {@code testInstance} and {@code testMethod}.
* <p>An attempt will be made to give each registered
* {@link TestExecutionListener} a chance to perform its pre-processing.
* If a listener throws an exception, however, the remaining registered
* listeners will <strong>not</strong> be called.
* @param testInstance the current test instance (never {@code null})
* @param testMethod the test method which is about to be executed on the
* test instance
* @throws Exception if a registered TestExecutionListener throws an exception
* @since 5.0
* @see #beforeTestMethod
* @see #afterTestMethod
* @see #beforeTestExecution
* @see #afterTestExecution
* @see #getTestExecutionListeners()
*/
public void beforeTestExecution(Object testInstance, Method testMethod) throws Exception {
String callbackName = "beforeTestExecution";
prepareForBeforeCallback(callbackName, testInstance, testMethod);
for (TestExecutionListener testExecutionListener : getTestExecutionListeners()) {
try {
testExecutionListener.beforeTestExecution(getTestContext());
}
catch (Throwable ex) {
handleBeforeException(ex, callbackName, testExecutionListener, testInstance, testMethod);
} }
} }
} }
/** /**
* Hook for post-processing a test <em>after</em> execution of the supplied * Hook for post-processing a test <em>immediately after</em> execution of
* {@link Method test method}, for example for tearing down test fixtures, * the {@linkplain java.lang.reflect.Method test method} in the supplied
* ending a transaction, etc. Should be called after any framework-specific * {@linkplain TestContext test context} &mdash; for example, for timing
* <em>after methods</em> (e.g., methods annotated with JUnit's * or logging purposes.
* <p>This method <strong>must</strong> be called before framework-specific
* <em>after</em> lifecycle callbacks (e.g., methods annotated with JUnit 4's
* {@link org.junit.After @After}). * {@link org.junit.After @After}).
* <p>The managed {@link TestContext} will be updated with the supplied * <p>The managed {@link TestContext} will be updated with the supplied
* {@code testInstance}, {@code testMethod}, and * {@code testInstance}, {@code testMethod}, and {@code exception}.
* {@code exception}. * <p>Each registered {@link TestExecutionListener} will be given a chance
* <p>Each registered {@link TestExecutionListener} will be given a chance to * to perform its post-processing. If a listener throws an exception, the
* post-process the test method execution. If a listener throws an * remaining registered listeners will still be called, but the first
* exception, the remaining registered listeners will still be called, but * exception thrown will be tracked and rethrown after all listeners have
* the first exception thrown will be tracked and rethrown after all * executed. Note that registered listeners will be executed in the opposite
* listeners have executed. Note that registered listeners will be executed * order in which they were registered.
* in the opposite order in which they were registered.
* @param testInstance the current test instance (never {@code null}) * @param testInstance the current test instance (never {@code null})
* @param testMethod the test method which has just been executed on the * @param testMethod the test method which has just been executed on the
* test instance * test instance
@ -301,15 +346,70 @@ public class TestContextManager {
* test method or by a TestExecutionListener, or {@code null} if none * test method or by a TestExecutionListener, or {@code null} if none
* was thrown * was thrown
* @throws Exception if a registered TestExecutionListener throws an exception * @throws Exception if a registered TestExecutionListener throws an exception
* @since 5.0
* @see #beforeTestMethod
* @see #afterTestMethod
* @see #beforeTestExecution
* @see #getTestExecutionListeners() * @see #getTestExecutionListeners()
*/ */
public void afterTestMethod(Object testInstance, Method testMethod, Throwable exception) throws Exception { public void afterTestExecution(Object testInstance, Method testMethod, Throwable exception) throws Exception {
Assert.notNull(testInstance, "Test instance must not be null"); String callbackName = "afterTestExecution";
if (logger.isTraceEnabled()) { prepareForAfterCallback(callbackName, testInstance, testMethod, exception);
logger.trace("afterTestMethod(): instance [" + testInstance + "], method [" + testMethod +
"], exception [" + exception + "]"); Throwable afterTestExecutionException = null;
// Traverse the TestExecutionListeners in reverse order to ensure proper
// "wrapper"-style execution of listeners.
for (TestExecutionListener testExecutionListener : getReversedTestExecutionListeners()) {
try {
testExecutionListener.afterTestExecution(getTestContext());
}
catch (Throwable ex) {
logException(ex, callbackName, testExecutionListener, testInstance, testMethod);
if (afterTestExecutionException == null) {
afterTestExecutionException = ex;
}
}
} }
getTestContext().updateState(testInstance, testMethod, exception); if (afterTestExecutionException != null) {
ReflectionUtils.rethrowException(afterTestExecutionException);
}
}
/**
* Hook for post-processing a test <em>after</em> execution of <em>after</em>
* lifecycle callbacks of the underlying test framework &mdash; for example,
* tearing down test fixtures, ending a transaction, etc.
* <p>This method <strong>must</strong> be called immediately after
* framework-specific <em>after</em> lifecycle callbacks (e.g., methods
* annotated with JUnit 4's {@link org.junit.After @After}). For historical
* reasons, this method is named {@code afterTestMethod}. Since the
* introduction of {@link #afterTestExecution}, a more suitable name for
* this method might be something like {@code afterTestTearDown} or
* {@code afterEach}; however, it is unfortunately impossible to rename
* this method due to backward compatibility concerns.
* <p>The managed {@link TestContext} will be updated with the supplied
* {@code testInstance}, {@code testMethod}, and {@code exception}.
* <p>Each registered {@link TestExecutionListener} will be given a chance
* to perform its post-processing. If a listener throws an exception, the
* remaining registered listeners will still be called, but the first
* exception thrown will be tracked and rethrown after all listeners have
* executed. Note that registered listeners will be executed in the opposite
* order in which they were registered.
* @param testInstance the current test instance (never {@code null})
* @param testMethod the test method which has just been executed on the
* test instance
* @param exception the exception that was thrown during execution of the
* test method or by a TestExecutionListener, or {@code null} if none
* was thrown
* @throws Exception if a registered TestExecutionListener throws an exception
* @see #beforeTestMethod
* @see #beforeTestExecution
* @see #afterTestExecution
* @see #getTestExecutionListeners()
*/
public void afterTestMethod(Object testInstance, Method testMethod, Throwable exception) throws Exception {
String callbackName = "afterTestMethod";
prepareForAfterCallback(callbackName, testInstance, testMethod, exception);
Throwable afterTestMethodException = null; Throwable afterTestMethodException = null;
// Traverse the TestExecutionListeners in reverse order to ensure proper // Traverse the TestExecutionListeners in reverse order to ensure proper
@ -319,11 +419,7 @@ public class TestContextManager {
testExecutionListener.afterTestMethod(getTestContext()); testExecutionListener.afterTestMethod(getTestContext());
} }
catch (Throwable ex) { catch (Throwable ex) {
if (logger.isWarnEnabled()) { logException(ex, callbackName, testExecutionListener, testInstance, testMethod);
logger.warn("Caught exception while allowing TestExecutionListener [" + testExecutionListener +
"] to process 'after' execution for test: method [" + testMethod + "], instance [" +
testInstance + "], exception [" + exception + "]", ex);
}
if (afterTestMethodException == null) { if (afterTestMethodException == null) {
afterTestMethodException = ex; afterTestMethodException = ex;
} }
@ -337,7 +433,7 @@ public class TestContextManager {
/** /**
* Hook for post-processing a test class <em>after</em> execution of all * Hook for post-processing a test class <em>after</em> execution of all
* tests within the class. Should be called after any framework-specific * tests within the class. Should be called after any framework-specific
* <em>after class methods</em> (e.g., methods annotated with JUnit's * <em>after class methods</em> (e.g., methods annotated with JUnit 4's
* {@link org.junit.AfterClass @AfterClass}). * {@link org.junit.AfterClass @AfterClass}).
* <p>Each registered {@link TestExecutionListener} will be given a chance to * <p>Each registered {@link TestExecutionListener} will be given a chance to
* post-process the test class. If a listener throws an exception, the * post-process the test class. If a listener throws an exception, the
@ -346,6 +442,7 @@ public class TestContextManager {
* executed. Note that registered listeners will be executed in the opposite * executed. Note that registered listeners will be executed in the opposite
* order in which they were registered. * order in which they were registered.
* @throws Exception if a registered TestExecutionListener throws an exception * @throws Exception if a registered TestExecutionListener throws an exception
* @since 3.0
* @see #getTestExecutionListeners() * @see #getTestExecutionListeners()
*/ */
public void afterTestClass() throws Exception { public void afterTestClass() throws Exception {
@ -363,10 +460,7 @@ public class TestContextManager {
testExecutionListener.afterTestClass(getTestContext()); testExecutionListener.afterTestClass(getTestContext());
} }
catch (Throwable ex) { catch (Throwable ex) {
if (logger.isWarnEnabled()) { logException(ex, "afterTestClass", testExecutionListener, testClass);
logger.warn("Caught exception while allowing TestExecutionListener [" + testExecutionListener +
"] to process 'after class' callback for test class [" + testClass + "]", ex);
}
if (afterTestClassException == null) { if (afterTestClassException == null) {
afterTestClassException = ex; afterTestClassException = ex;
} }
@ -377,4 +471,46 @@ public class TestContextManager {
} }
} }
private void prepareForBeforeCallback(String callbackName, Object testInstance, Method testMethod) {
Assert.notNull(testInstance, "Test instance must not be null");
if (logger.isTraceEnabled()) {
logger.trace(String.format("%s(): instance [%s], method [%s]", callbackName, testInstance, testMethod));
}
getTestContext().updateState(testInstance, testMethod, null);
}
private void prepareForAfterCallback(String callbackName, Object testInstance, Method testMethod,
Throwable exception) {
Assert.notNull(testInstance, "Test instance must not be null");
if (logger.isTraceEnabled()) {
logger.trace(String.format("%s(): instance [%s], method [%s], exception [%s]", callbackName, testInstance,
testMethod, exception));
}
getTestContext().updateState(testInstance, testMethod, exception);
}
private void handleBeforeException(Throwable ex, String callbackName, TestExecutionListener testExecutionListener,
Object testInstance, Method testMethod) throws Exception {
logException(ex, callbackName, testExecutionListener, testInstance, testMethod);
ReflectionUtils.rethrowException(ex);
}
private void logException(Throwable ex, String callbackName, TestExecutionListener testExecutionListener,
Class<?> testClass) {
if (logger.isWarnEnabled()) {
logger.warn(String.format("Caught exception while invoking '%s' callback on " +
"TestExecutionListener [%s] for test class [%s]", callbackName, testExecutionListener,
testClass), ex);
}
}
private void logException(Throwable ex, String callbackName, TestExecutionListener testExecutionListener,
Object testInstance, Method testMethod) {
if (logger.isWarnEnabled()) {
logger.warn(String.format("Caught exception while invoking '%s' callback on " +
"TestExecutionListener [%s] for test method [%s] and test instance [%s]",
callbackName, testExecutionListener, testMethod, testInstance), ex);
}
}
} }

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

@ -55,6 +55,7 @@ package org.springframework.test.context;
* @author Sam Brannen * @author Sam Brannen
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 2.5 * @since 2.5
* @see TestContextManager
* @see org.springframework.test.context.support.AbstractTestExecutionListener * @see org.springframework.test.context.support.AbstractTestExecutionListener
*/ */
public interface TestExecutionListener { public interface TestExecutionListener {
@ -70,6 +71,7 @@ public interface TestExecutionListener {
* concrete classes as necessary. * concrete classes as necessary.
* @param testContext the test context for the test; never {@code null} * @param testContext the test context for the test; never {@code null}
* @throws Exception allows any exception to propagate * @throws Exception allows any exception to propagate
* @since 3.0
*/ */
default void beforeTestClass(TestContext testContext) throws Exception { default void beforeTestClass(TestContext testContext) throws Exception {
/* no-op */ /* no-op */
@ -90,34 +92,90 @@ public interface TestExecutionListener {
} }
/** /**
* Pre-processes a test <em>before</em> execution of the * Pre-processes a test <em>before</em> execution of <em>before</em>
* lifecycle callbacks of the underlying test framework &mdash; for example,
* by setting up test fixtures.
* <p>This method <strong>must</strong> be called immediately prior to
* framework-specific <em>before</em> lifecycle callbacks. For historical
* reasons, this method is named {@code beforeTestMethod}. Since the
* introduction of {@link #beforeTestExecution}, a more suitable name for
* this method might be something like {@code beforeTestSetUp} or
* {@code beforeEach}; however, it is unfortunately impossible to rename
* this method due to backward compatibility concerns.
* <p>The default implementation is <em>empty</em>. Can be overridden by
* concrete classes as necessary.
* @param testContext the test context in which the test method will be
* executed; never {@code null}
* @throws Exception allows any exception to propagate
* @see #afterTestMethod
* @see #beforeTestExecution
* @see #afterTestExecution
*/
default void beforeTestMethod(TestContext testContext) throws Exception {
/* no-op */
}
/**
* Pre-processes a test <em>immediately before</em> execution of the
* {@link java.lang.reflect.Method test method} in the supplied * {@link java.lang.reflect.Method test method} in the supplied
* {@link TestContext test context}, for example by setting up test * {@link TestContext test context} &mdash; for example, for timing
* fixtures. * or logging purposes.
* <p>This method should be called immediately prior to framework-specific * <p>This method <strong>must</strong> be called after framework-specific
* <em>before</em> lifecycle callbacks. * <em>before</em> lifecycle callbacks.
* <p>The default implementation is <em>empty</em>. Can be overridden by * <p>The default implementation is <em>empty</em>. Can be overridden by
* concrete classes as necessary. * concrete classes as necessary.
* @param testContext the test context in which the test method will be * @param testContext the test context in which the test method will be
* executed; never {@code null} * executed; never {@code null}
* @throws Exception allows any exception to propagate * @throws Exception allows any exception to propagate
* @since 5.0
* @see #beforeTestMethod
* @see #afterTestMethod
* @see #afterTestExecution
*/ */
default void beforeTestMethod(TestContext testContext) throws Exception { default void beforeTestExecution(TestContext testContext) throws Exception {
/* no-op */ /* no-op */
} }
/** /**
* Post-processes a test <em>after</em> execution of the * Post-processes a test <em>immediately after</em> execution of the
* {@link java.lang.reflect.Method test method} in the supplied * {@link java.lang.reflect.Method test method} in the supplied
* {@link TestContext test context}, for example by tearing down test * {@link TestContext test context} &mdash; for example, for timing
* fixtures. * or logging purposes.
* <p>This method should be called immediately after framework-specific * <p>This method <strong>must</strong> be called before framework-specific
* <em>after</em> lifecycle callbacks. * <em>after</em> lifecycle callbacks.
* <p>The default implementation is <em>empty</em>. Can be overridden by * <p>The default implementation is <em>empty</em>. Can be overridden by
* concrete classes as necessary. * concrete classes as necessary.
* @param testContext the test context in which the test method will be
* executed; never {@code null}
* @throws Exception allows any exception to propagate
* @since 5.0
* @see #beforeTestMethod
* @see #afterTestMethod
* @see #beforeTestExecution
*/
default void afterTestExecution(TestContext testContext) throws Exception {
/* no-op */
}
/**
* Post-processes a test <em>after</em> execution of <em>after</em>
* lifecycle callbacks of the underlying test framework &mdash; for example,
* by tearing down test fixtures.
* <p>This method <strong>must</strong> be called immediately after
* framework-specific <em>after</em> lifecycle callbacks. For historical
* reasons, this method is named {@code afterTestMethod}. Since the
* introduction of {@link #afterTestExecution}, a more suitable name for
* this method might be something like {@code afterTestTearDown} or
* {@code afterEach}; however, it is unfortunately impossible to rename
* this method due to backward compatibility concerns.
* <p>The default implementation is <em>empty</em>. Can be overridden by
* concrete classes as necessary.
* @param testContext the test context in which the test method was * @param testContext the test context in which the test method was
* executed; never {@code null} * executed; never {@code null}
* @throws Exception allows any exception to propagate * @throws Exception allows any exception to propagate
* @see #beforeTestMethod
* @see #beforeTestExecution
* @see #afterTestExecution
*/ */
default void afterTestMethod(TestContext testContext) throws Exception { default void afterTestMethod(TestContext testContext) throws Exception {
/* no-op */ /* no-op */
@ -134,6 +192,7 @@ public interface TestExecutionListener {
* concrete classes as necessary. * concrete classes as necessary.
* @param testContext the test context for the test; never {@code null} * @param testContext the test context for the test; never {@code null}
* @throws Exception allows any exception to propagate * @throws Exception allows any exception to propagate
* @since 3.0
*/ */
default void afterTestClass(TestContext testContext) throws Exception { default void afterTestClass(TestContext testContext) throws Exception {
/* no-op */ /* no-op */

26
spring-test/src/main/java/org/springframework/test/context/junit/jupiter/SpringExtension.java

@ -23,8 +23,10 @@ import java.lang.reflect.Parameter;
import org.junit.jupiter.api.extension.AfterAllCallback; import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.AfterEachCallback; import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback; import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback; import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
import org.junit.jupiter.api.extension.ContainerExtensionContext; import org.junit.jupiter.api.extension.ContainerExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace; import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
@ -54,7 +56,8 @@ import org.springframework.util.Assert;
* @see org.springframework.test.context.TestContextManager * @see org.springframework.test.context.TestContextManager
*/ */
public class SpringExtension implements BeforeAllCallback, AfterAllCallback, TestInstancePostProcessor, public class SpringExtension implements BeforeAllCallback, AfterAllCallback, TestInstancePostProcessor,
BeforeEachCallback, AfterEachCallback, ParameterResolver { BeforeEachCallback, AfterEachCallback, BeforeTestExecutionCallback, AfterTestExecutionCallback,
ParameterResolver {
/** /**
* {@link Namespace} in which {@code TestContextManagers} are stored, keyed * {@link Namespace} in which {@code TestContextManagers} are stored, keyed
@ -101,6 +104,27 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
getTestContextManager(context).beforeTestMethod(testInstance, testMethod); getTestContextManager(context).beforeTestMethod(testInstance, testMethod);
} }
/**
* Delegates to {@link TestContextManager#beforeTestExecution}.
*/
@Override
public void beforeTestExecution(TestExtensionContext context) throws Exception {
Object testInstance = context.getTestInstance();
Method testMethod = context.getTestMethod().get();
getTestContextManager(context).beforeTestExecution(testInstance, testMethod);
}
/**
* Delegates to {@link TestContextManager#afterTestExecution}.
*/
@Override
public void afterTestExecution(TestExtensionContext context) throws Exception {
Object testInstance = context.getTestInstance();
Method testMethod = context.getTestMethod().get();
Throwable testException = context.getTestException().orElse(null);
getTestContextManager(context).afterTestExecution(testInstance, testMethod, testException);
}
/** /**
* Delegates to {@link TestContextManager#afterTestMethod}. * Delegates to {@link TestContextManager#afterTestMethod}.
*/ */

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

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

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

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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; 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 * supports instance-level and method-level features of the
* <em>Spring TestContext Framework</em> in standard JUnit tests by means * <em>Spring TestContext Framework</em> in standard JUnit tests by means
* of the {@link TestContextManager} and associated support classes and * 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>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 Sam Brannen
* @author Philippe Marschall * @author Philippe Marschall
* @since 4.2 * @since 4.2

100
spring-test/src/main/java/org/springframework/test/context/junit4/statements/RunAfterTestExecutionCallbacks.java

@ -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);
}
}

76
spring-test/src/main/java/org/springframework/test/context/junit4/statements/RunBeforeTestExecutionCallbacks.java

@ -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();
}
}

69
spring-test/src/main/java/org/springframework/test/context/testng/AbstractTestNGSpringContextTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -49,12 +49,11 @@ import org.testng.annotations.BeforeMethod;
* <p>Concrete subclasses: * <p>Concrete subclasses:
* <ul> * <ul>
* <li>Typically declare a class-level {@link ContextConfiguration * <li>Typically declare a class-level {@link ContextConfiguration
* &#064;ContextConfiguration} annotation to configure the {@link ApplicationContext * &#064;ContextConfiguration} annotation to configure the {@linkplain ApplicationContext
* application context} {@link ContextConfiguration#locations() resource locations} * application context} {@linkplain ContextConfiguration#locations() resource locations}
* or {@link ContextConfiguration#classes() annotated classes}. <em>If your test * or {@linkplain ContextConfiguration#classes() annotated classes}. <em>If your test
* does not need to load an application context, you may choose to omit the * does not need to load an application context, you may choose to omit the
* {@link ContextConfiguration &#064;ContextConfiguration} declaration and to * {@code @ContextConfiguration} declaration and to configure the appropriate
* configure the appropriate
* {@link org.springframework.test.context.TestExecutionListener TestExecutionListeners} * {@link org.springframework.test.context.TestExecutionListener TestExecutionListeners}
* manually.</em></li> * manually.</em></li>
* <li>Must have constructors which either implicitly or explicitly delegate to * <li>Must have constructors which either implicitly or explicitly delegate to
@ -105,7 +104,7 @@ public abstract class AbstractTestNGSpringContextTests implements IHookable, App
/** /**
* Construct a new AbstractTestNGSpringContextTests instance and initialize * Construct a new AbstractTestNGSpringContextTests instance and initialize
* the internal {@link TestContextManager} for the current test. * the internal {@link TestContextManager} for the current test class.
*/ */
public AbstractTestNGSpringContextTests() { public AbstractTestNGSpringContextTests() {
this.testContextManager = new TestContextManager(getClass()); this.testContextManager = new TestContextManager(getClass());
@ -124,7 +123,7 @@ public abstract class AbstractTestNGSpringContextTests implements IHookable, App
/** /**
* Delegates to the configured {@link TestContextManager} to call * Delegates to the configured {@link TestContextManager} to call
* {@link TestContextManager#beforeTestClass() 'before test class'} * {@linkplain TestContextManager#beforeTestClass() 'before test class'}
* callbacks. * callbacks.
* *
* @throws Exception if a registered TestExecutionListener throws an * @throws Exception if a registered TestExecutionListener throws an
@ -137,7 +136,7 @@ public abstract class AbstractTestNGSpringContextTests implements IHookable, App
/** /**
* Delegates to the configured {@link TestContextManager} to * Delegates to the configured {@link TestContextManager} to
* {@link TestContextManager#prepareTestInstance(Object) prepare} this test * {@linkplain TestContextManager#prepareTestInstance(Object) prepare} this test
* instance prior to execution of any individual tests, for example for * instance prior to execution of any individual tests, for example for
* injecting dependencies, etc. * injecting dependencies, etc.
* *
@ -151,11 +150,11 @@ public abstract class AbstractTestNGSpringContextTests implements IHookable, App
/** /**
* Delegates to the configured {@link TestContextManager} to * Delegates to the configured {@link TestContextManager} to
* {@link TestContextManager#beforeTestMethod(Object,Method) pre-process} * {@linkplain TestContextManager#beforeTestMethod(Object,Method) pre-process}
* the test method before the actual test is executed. * the test method before the actual test is executed.
* *
* @param testMethod the test method which is about to be executed. * @param testMethod the test method which is about to be executed
* @throws Exception allows all exceptions to propagate. * @throws Exception allows all exceptions to propagate
*/ */
@BeforeMethod(alwaysRun = true) @BeforeMethod(alwaysRun = true)
protected void springTestContextBeforeTestMethod(Method testMethod) throws Exception { protected void springTestContextBeforeTestMethod(Method testMethod) throws Exception {
@ -163,27 +162,45 @@ public abstract class AbstractTestNGSpringContextTests implements IHookable, App
} }
/** /**
* Delegates to the {@link IHookCallBack#runTestMethod(ITestResult) test * Delegates to the {@linkplain IHookCallBack#runTestMethod(ITestResult) test
* method} in the supplied {@code callback} to execute the actual test * method} in the supplied {@code callback} to execute the actual test
* and then tracks the exception thrown during test execution, if any. * and then tracks the exception thrown during test execution, if any.
* *
* @see org.testng.IHookable#run(org.testng.IHookCallBack, * @see org.testng.IHookable#run(IHookCallBack, ITestResult)
* org.testng.ITestResult)
*/ */
@Override @Override
public void run(IHookCallBack callBack, ITestResult testResult) { public void run(IHookCallBack callBack, ITestResult testResult) {
callBack.runTestMethod(testResult); Method testMethod = testResult.getMethod().getConstructorOrMethod().getMethod();
boolean beforeCallbacksExecuted = false;
Throwable testResultException = testResult.getThrowable(); try {
if (testResultException instanceof InvocationTargetException) { this.testContextManager.beforeTestExecution(this, testMethod);
testResultException = ((InvocationTargetException) testResultException).getCause(); beforeCallbacksExecuted = true;
}
catch (Throwable ex) {
testResult.setThrowable(ex);
this.testException = ex;
}
if (beforeCallbacksExecuted) {
callBack.runTestMethod(testResult);
this.testException = getTestResultException(testResult);
}
try {
this.testContextManager.afterTestExecution(this, testMethod, this.testException);
}
catch (Throwable ex) {
if (this.testException == null) {
testResult.setThrowable(ex);
this.testException = ex;
}
} }
this.testException = testResultException;
} }
/** /**
* Delegates to the configured {@link TestContextManager} to * Delegates to the configured {@link TestContextManager} to
* {@link TestContextManager#afterTestMethod(Object, Method, Throwable) * {@linkplain TestContextManager#afterTestMethod(Object, Method, Throwable)
* post-process} the test method after the actual test has executed. * post-process} the test method after the actual test has executed.
* *
* @param testMethod the test method which has just been executed on the * @param testMethod the test method which has just been executed on the
@ -202,7 +219,7 @@ public abstract class AbstractTestNGSpringContextTests implements IHookable, App
/** /**
* Delegates to the configured {@link TestContextManager} to call * Delegates to the configured {@link TestContextManager} to call
* {@link TestContextManager#afterTestClass() 'after test class'} callbacks. * {@linkplain TestContextManager#afterTestClass() 'after test class'} callbacks.
* *
* @throws Exception if a registered TestExecutionListener throws an * @throws Exception if a registered TestExecutionListener throws an
* exception * exception
@ -212,4 +229,12 @@ public abstract class AbstractTestNGSpringContextTests implements IHookable, App
this.testContextManager.afterTestClass(); this.testContextManager.afterTestClass();
} }
private Throwable getTestResultException(ITestResult testResult) {
Throwable testResultException = testResult.getThrowable();
if (testResultException instanceof InvocationTargetException) {
testResultException = ((InvocationTargetException) testResultException).getCause();
}
return testResultException;
}
} }

174
spring-test/src/test/java/org/springframework/test/context/TestContextManagerTests.java

@ -21,17 +21,8 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.springframework.core.style.ToStringCreator;
import org.springframework.test.context.support.AbstractTestExecutionListener;
import static org.junit.Assert.*; import static org.junit.Assert.*;
/** /**
@ -44,94 +35,77 @@ import static org.junit.Assert.*;
*/ */
public class TestContextManagerTests { public class TestContextManagerTests {
private static final String FIRST = "veni"; private static final List<String> executionOrder = new ArrayList<>();
private static final String SECOND = "vidi";
private static final String THIRD = "vici";
private static final List<String> afterTestMethodCalls = new ArrayList<>();
private static final List<String> beforeTestMethodCalls = new ArrayList<>();
protected static final Log logger = LogFactory.getLog(TestContextManagerTests.class);
private final TestContextManager testContextManager = new TestContextManager(ExampleTestCase.class); private final TestContextManager testContextManager = new TestContextManager(ExampleTestCase.class);
private final Method testMethod;
private Method getTestMethod() throws NoSuchMethodException { {
return ExampleTestCase.class.getDeclaredMethod("exampleTestMethod", (Class<?>[]) null); try {
} this.testMethod = ExampleTestCase.class.getDeclaredMethod("exampleTestMethod");
/**
* Asserts the <em>execution order</em> of 'before' and 'after' test method
* calls on {@link TestExecutionListener listeners} registered for the
* configured {@link TestContextManager}.
*
* @see #beforeTestMethodCalls
* @see #afterTestMethodCalls
*/
private static void assertExecutionOrder(List<String> expectedBeforeTestMethodCalls,
List<String> expectedAfterTestMethodCalls, final String usageContext) {
if (expectedBeforeTestMethodCalls == null) {
expectedBeforeTestMethodCalls = new ArrayList<>();
}
if (expectedAfterTestMethodCalls == null) {
expectedAfterTestMethodCalls = new ArrayList<>();
} }
catch (Exception ex) {
if (logger.isDebugEnabled()) { throw new RuntimeException(ex);
for (String listenerName : beforeTestMethodCalls) {
logger.debug("'before' listener [" + listenerName + "] (" + usageContext + ").");
}
for (String listenerName : afterTestMethodCalls) {
logger.debug("'after' listener [" + listenerName + "] (" + usageContext + ").");
}
} }
assertTrue("Verifying execution order of 'before' listeners' (" + usageContext + ").",
expectedBeforeTestMethodCalls.equals(beforeTestMethodCalls));
assertTrue("Verifying execution order of 'after' listeners' (" + usageContext + ").",
expectedAfterTestMethodCalls.equals(afterTestMethodCalls));
} }
@BeforeClass
public static void setUpBeforeClass() throws Exception {
beforeTestMethodCalls.clear();
afterTestMethodCalls.clear();
assertExecutionOrder(null, null, "BeforeClass");
}
/**
* Verifies the expected {@link TestExecutionListener}
* <em>execution order</em> after all test methods have completed.
*/
@AfterClass
public static void verifyListenerExecutionOrderAfterClass() throws Exception {
assertExecutionOrder(Arrays.<String> asList(FIRST, SECOND, THIRD),
Arrays.<String> asList(THIRD, SECOND, FIRST), "AfterClass");
}
@Before
public void setUpTestContextManager() throws Throwable {
assertEquals("Verifying the number of registered TestExecutionListeners.", 3,
this.testContextManager.getTestExecutionListeners().size());
this.testContextManager.beforeTestMethod(new ExampleTestCase(), getTestMethod());
}
/**
* Verifies the expected {@link TestExecutionListener}
* <em>execution order</em> within a test method.
*
* @see #verifyListenerExecutionOrderAfterClass()
*/
@Test @Test
public void verifyListenerExecutionOrderWithinTestMethod() { public void listenerExecutionOrder() throws Exception {
assertExecutionOrder(Arrays.<String> asList(FIRST, SECOND, THIRD), null, "Test"); // @formatter:off
assertEquals("Registered TestExecutionListeners", 3, this.testContextManager.getTestExecutionListeners().size());
this.testContextManager.beforeTestMethod(this, this.testMethod);
assertExecutionOrder("beforeTestMethod",
"beforeTestMethod-1",
"beforeTestMethod-2",
"beforeTestMethod-3"
);
this.testContextManager.beforeTestExecution(this, this.testMethod);
assertExecutionOrder("beforeTestExecution",
"beforeTestMethod-1",
"beforeTestMethod-2",
"beforeTestMethod-3",
"beforeTestExecution-1",
"beforeTestExecution-2",
"beforeTestExecution-3"
);
this.testContextManager.afterTestExecution(this, this.testMethod, null);
assertExecutionOrder("afterTestExecution",
"beforeTestMethod-1",
"beforeTestMethod-2",
"beforeTestMethod-3",
"beforeTestExecution-1",
"beforeTestExecution-2",
"beforeTestExecution-3",
"afterTestExecution-3",
"afterTestExecution-2",
"afterTestExecution-1"
);
this.testContextManager.afterTestMethod(this, this.testMethod, null);
assertExecutionOrder("afterTestMethod",
"beforeTestMethod-1",
"beforeTestMethod-2",
"beforeTestMethod-3",
"beforeTestExecution-1",
"beforeTestExecution-2",
"beforeTestExecution-3",
"afterTestExecution-3",
"afterTestExecution-2",
"afterTestExecution-1",
"afterTestMethod-3",
"afterTestMethod-2",
"afterTestMethod-1"
);
// @formatter:on
} }
@After private static void assertExecutionOrder(String usageContext, String... expectedBeforeTestMethodCalls) {
public void tearDownTestContextManager() throws Throwable { assertEquals("execution order (" + usageContext + ") ==>", Arrays.asList(expectedBeforeTestMethodCalls),
this.testContextManager.afterTestMethod(new ExampleTestCase(), getTestMethod(), null); executionOrder);
} }
@ -140,53 +114,57 @@ public class TestContextManagerTests {
@SuppressWarnings("unused") @SuppressWarnings("unused")
public void exampleTestMethod() { public void exampleTestMethod() {
assertTrue(true);
} }
} }
private static class NamedTestExecutionListener extends AbstractTestExecutionListener { private static class NamedTestExecutionListener implements TestExecutionListener {
private final String name; private final String name;
public NamedTestExecutionListener(final String name) { public NamedTestExecutionListener(String name) {
this.name = name; this.name = name;
} }
@Override @Override
public void beforeTestMethod(final TestContext testContext) { public void beforeTestMethod(TestContext testContext) {
beforeTestMethodCalls.add(this.name); executionOrder.add("beforeTestMethod-" + this.name);
}
@Override
public void beforeTestExecution(TestContext testContext) {
executionOrder.add("beforeTestExecution-" + this.name);
} }
@Override @Override
public void afterTestMethod(final TestContext testContext) { public void afterTestExecution(TestContext testContext) {
afterTestMethodCalls.add(this.name); executionOrder.add("afterTestExecution-" + this.name);
} }
@Override @Override
public String toString() { public void afterTestMethod(TestContext testContext) {
return new ToStringCreator(this).append("name", this.name).toString(); executionOrder.add("afterTestMethod-" + this.name);
} }
} }
private static class FirstTel extends NamedTestExecutionListener { private static class FirstTel extends NamedTestExecutionListener {
public FirstTel() { public FirstTel() {
super(FIRST); super("1");
} }
} }
private static class SecondTel extends NamedTestExecutionListener { private static class SecondTel extends NamedTestExecutionListener {
public SecondTel() { public SecondTel() {
super(SECOND); super("2");
} }
} }
private static class ThirdTel extends NamedTestExecutionListener { private static class ThirdTel extends NamedTestExecutionListener {
public ThirdTel() { public ThirdTel() {
super(THIRD); super("3");
} }
} }

264
spring-test/src/test/java/org/springframework/test/context/junit/jupiter/FailingBeforeAndAfterMethodsSpringExtensionTestCase.java

@ -0,0 +1,264 @@
/*
* 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.junit.jupiter;
import java.util.stream.Stream;
import javax.sql.DataSource;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.core.LauncherFactory;
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
import org.junit.platform.launcher.listeners.TestExecutionSummary;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestExecutionListener;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.transaction.AfterTransaction;
import org.springframework.test.context.transaction.BeforeTransaction;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.DynamicTest.*;
import static org.junit.platform.engine.discovery.DiscoverySelectors.*;
import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.*;
/**
* Integration tests which verify that '<i>before</i>' and '<i>after</i>'
* methods of {@link TestExecutionListener TestExecutionListeners} as well as
* {@code @BeforeTransaction} and {@code @AfterTransaction} methods can fail
* tests run via the {@link SpringExtension} in a JUnit 5 (Jupiter) environment.
*
* <p>See: <a href="https://jira.spring.io/browse/SPR-3960" target="_blank">SPR-3960</a>
* and <a href="https://jira.spring.io/browse/SPR-4365" target="_blank">SPR-4365</a>.
*
* <p>Indirectly, this class also verifies that all {@code TestExecutionListener}
* lifecycle callbacks are called.
*
* @author Sam Brannen
* @since 5.0
*/
class FailingBeforeAndAfterMethodsSpringExtensionTestCase {
private static Stream<Class<?>> testClasses() {
// @formatter:off
return Stream.of(
AlwaysFailingBeforeTestClassTestCase.class,
AlwaysFailingAfterTestClassTestCase.class,
AlwaysFailingPrepareTestInstanceTestCase.class,
AlwaysFailingBeforeTestMethodTestCase.class,
AlwaysFailingBeforeTestExecutionTestCase.class,
AlwaysFailingAfterTestExecutionTestCase.class,
AlwaysFailingAfterTestMethodTestCase.class,
FailingBeforeTransactionTestCase.class,
FailingAfterTransactionTestCase.class);
// @formatter:on
}
@TestFactory
Stream<DynamicTest> generateTests() throws Exception {
return testClasses().map(clazz -> dynamicTest(clazz.getSimpleName(), () -> runTestAndAssertCounters(clazz)));
}
private void runTestAndAssertCounters(Class<?> testClass) {
Launcher launcher = LauncherFactory.create();
SummaryGeneratingListener listener = new SummaryGeneratingListener();
launcher.registerTestExecutionListeners(listener);
launcher.execute(request().selectors(selectClass(testClass)).build());
TestExecutionSummary summary = listener.getSummary();
String name = testClass.getSimpleName();
int expectedStartedCount = getExpectedStartedCount(testClass);
int expectedSucceededCount = getExpectedSucceededCount(testClass);
int expectedFailedCount = getExpectedFailedCount(testClass);
// @formatter:off
assertAll(
() -> assertEquals(1, summary.getTestsFoundCount(), () -> name + ": tests found"),
() -> assertEquals(0, summary.getTestsSkippedCount(), () -> name + ": tests skipped"),
() -> assertEquals(0, summary.getTestsAbortedCount(), () -> name + ": tests aborted"),
() -> assertEquals(expectedStartedCount, summary.getTestsStartedCount(), () -> name + ": tests started"),
() -> assertEquals(expectedSucceededCount, summary.getTestsSucceededCount(), () -> name + ": tests succeeded"),
() -> assertEquals(expectedFailedCount, summary.getTestsFailedCount(), () -> name + ": tests failed")
);
// @formatter:on
}
private int getExpectedStartedCount(Class<?> testClass) {
return (testClass == AlwaysFailingBeforeTestClassTestCase.class ? 0 : 1);
}
private int getExpectedSucceededCount(Class<?> testClass) {
return (testClass == AlwaysFailingAfterTestClassTestCase.class ? 1 : 0);
}
private int getExpectedFailedCount(Class<?> testClass) {
if (testClass == AlwaysFailingBeforeTestClassTestCase.class
|| testClass == AlwaysFailingAfterTestClassTestCase.class) {
return 0;
}
return 1;
}
// -------------------------------------------------------------------
private static class AlwaysFailingBeforeTestClassTestExecutionListener implements TestExecutionListener {
@Override
public void beforeTestClass(TestContext testContext) {
fail("always failing beforeTestClass()");
}
}
private static class AlwaysFailingAfterTestClassTestExecutionListener implements TestExecutionListener {
@Override
public void afterTestClass(TestContext testContext) {
fail("always failing afterTestClass()");
}
}
private static class AlwaysFailingPrepareTestInstanceTestExecutionListener implements TestExecutionListener {
@Override
public void prepareTestInstance(TestContext testContext) throws Exception {
fail("always failing prepareTestInstance()");
}
}
private static class AlwaysFailingBeforeTestMethodTestExecutionListener implements TestExecutionListener {
@Override
public void beforeTestMethod(TestContext testContext) {
fail("always failing beforeTestMethod()");
}
}
private static class AlwaysFailingBeforeTestExecutionTestExecutionListener implements TestExecutionListener {
@Override
public void beforeTestExecution(TestContext testContext) {
fail("always failing beforeTestExecution()");
}
}
private static class AlwaysFailingAfterTestMethodTestExecutionListener implements TestExecutionListener {
@Override
public void afterTestMethod(TestContext testContext) {
fail("always failing afterTestMethod()");
}
}
private static class AlwaysFailingAfterTestExecutionTestExecutionListener implements TestExecutionListener {
@Override
public void afterTestExecution(TestContext testContext) {
fail("always failing afterTestExecution()");
}
}
@ExtendWith(SpringExtension.class)
private static abstract class BaseTestCase {
@Test
void testNothing() {
}
}
@TestExecutionListeners(AlwaysFailingBeforeTestClassTestExecutionListener.class)
private static class AlwaysFailingBeforeTestClassTestCase extends BaseTestCase {
}
@TestExecutionListeners(AlwaysFailingAfterTestClassTestExecutionListener.class)
private static class AlwaysFailingAfterTestClassTestCase extends BaseTestCase {
}
@TestExecutionListeners(AlwaysFailingPrepareTestInstanceTestExecutionListener.class)
private static class AlwaysFailingPrepareTestInstanceTestCase extends BaseTestCase {
}
@TestExecutionListeners(AlwaysFailingBeforeTestMethodTestExecutionListener.class)
private static class AlwaysFailingBeforeTestMethodTestCase extends BaseTestCase {
}
@TestExecutionListeners(AlwaysFailingBeforeTestExecutionTestExecutionListener.class)
private static class AlwaysFailingBeforeTestExecutionTestCase extends BaseTestCase {
}
@TestExecutionListeners(AlwaysFailingAfterTestExecutionTestExecutionListener.class)
private static class AlwaysFailingAfterTestExecutionTestCase extends BaseTestCase {
}
@TestExecutionListeners(AlwaysFailingAfterTestMethodTestExecutionListener.class)
private static class AlwaysFailingAfterTestMethodTestCase extends BaseTestCase {
}
@SpringJUnitConfig(DatabaseConfig.class)
@Transactional
private static class FailingBeforeTransactionTestCase {
@Test
void testNothing() {
}
@BeforeTransaction
void beforeTransaction() {
fail("always failing beforeTransaction()");
}
}
@SpringJUnitConfig(DatabaseConfig.class)
@Transactional
private static class FailingAfterTransactionTestCase {
@Test
void testNothing() {
}
@AfterTransaction
void afterTransaction() {
fail("always failing afterTransaction()");
}
}
@Configuration
private static class DatabaseConfig {
@Bean
PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
@Bean
DataSource dataSource() {
return new EmbeddedDatabaseBuilder().generateUniqueName(true).build();
}
}
}

61
spring-test/src/test/java/org/springframework/test/context/junit4/FailingBeforeAndAfterMethodsJUnitTests.java → spring-test/src/test/java/org/springframework/test/context/junit4/FailingBeforeAndAfterMethodsSpringRunnerTests.java

@ -27,7 +27,6 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestContext; import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestExecutionListener; import org.springframework.test.context.TestExecutionListener;
import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.support.AbstractTestExecutionListener;
import org.springframework.test.context.transaction.AfterTransaction; import org.springframework.test.context.transaction.AfterTransaction;
import org.springframework.test.context.transaction.BeforeTransaction; import org.springframework.test.context.transaction.BeforeTransaction;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -37,26 +36,21 @@ import static org.junit.Assert.*;
import static org.springframework.test.context.junit4.JUnitTestingUtils.*; import static org.springframework.test.context.junit4.JUnitTestingUtils.*;
/** /**
* JUnit 4 based integration test for verifying that '<i>before</i>' and '<i>after</i>' * Integration tests which verify that '<i>before</i>' and '<i>after</i>'
* methods of {@link TestExecutionListener TestExecutionListeners} as well as * methods of {@link TestExecutionListener TestExecutionListeners} as well as
* {@link BeforeTransaction &#064;BeforeTransaction} and * {@code @BeforeTransaction} and {@code @AfterTransaction} methods can fail
* {@link AfterTransaction &#064;AfterTransaction} methods can fail a test in a * tests run via the {@link SpringRunner} in a JUnit 4 environment.
* JUnit environment, as requested in
* <a href="https://jira.spring.io/browse/SPR-3960" target="_blank">SPR-3960</a>.
* *
* <p>Indirectly, this class also verifies that all {@link TestExecutionListener} * <p>See: <a href="https://jira.spring.io/browse/SPR-3960" target="_blank">SPR-3960</a>.
* lifecycle callbacks are called.
* *
* <p>As of Spring 3.0, this class also tests support for the new * <p>Indirectly, this class also verifies that all {@code TestExecutionListener}
* {@link TestExecutionListener#beforeTestClass(TestContext) beforeTestClass()} * lifecycle callbacks are called.
* and {@link TestExecutionListener#afterTestClass(TestContext)
* afterTestClass()} lifecycle callback methods.
* *
* @author Sam Brannen * @author Sam Brannen
* @since 2.5 * @since 2.5
*/ */
@RunWith(Parameterized.class) @RunWith(Parameterized.class)
public class FailingBeforeAndAfterMethodsJUnitTests { public class FailingBeforeAndAfterMethodsSpringRunnerTests {
protected final Class<?> clazz; protected final Class<?> clazz;
@ -68,13 +62,15 @@ public class FailingBeforeAndAfterMethodsJUnitTests {
AlwaysFailingAfterTestClassTestCase.class.getSimpleName(),// AlwaysFailingAfterTestClassTestCase.class.getSimpleName(),//
AlwaysFailingPrepareTestInstanceTestCase.class.getSimpleName(),// AlwaysFailingPrepareTestInstanceTestCase.class.getSimpleName(),//
AlwaysFailingBeforeTestMethodTestCase.class.getSimpleName(),// AlwaysFailingBeforeTestMethodTestCase.class.getSimpleName(),//
AlwaysFailingBeforeTestExecutionTestCase.class.getSimpleName(), //
AlwaysFailingAfterTestExecutionTestCase.class.getSimpleName(), //
AlwaysFailingAfterTestMethodTestCase.class.getSimpleName(),// AlwaysFailingAfterTestMethodTestCase.class.getSimpleName(),//
FailingBeforeTransactionTestCase.class.getSimpleName(),// FailingBeforeTransactionTestCase.class.getSimpleName(),//
FailingAfterTransactionTestCase.class.getSimpleName() // FailingAfterTransactionTestCase.class.getSimpleName() //
}; };
} }
public FailingBeforeAndAfterMethodsJUnitTests(String testClassName) throws Exception { public FailingBeforeAndAfterMethodsSpringRunnerTests(String testClassName) throws Exception {
this.clazz = ClassUtils.forName(getClass().getName() + "." + testClassName, getClass().getClassLoader()); this.clazz = ClassUtils.forName(getClass().getName() + "." + testClassName, getClass().getClassLoader());
} }
@ -93,7 +89,7 @@ public class FailingBeforeAndAfterMethodsJUnitTests {
// ------------------------------------------------------------------- // -------------------------------------------------------------------
protected static class AlwaysFailingBeforeTestClassTestExecutionListener extends AbstractTestExecutionListener { protected static class AlwaysFailingBeforeTestClassTestExecutionListener implements TestExecutionListener {
@Override @Override
public void beforeTestClass(TestContext testContext) { public void beforeTestClass(TestContext testContext) {
@ -101,7 +97,7 @@ public class FailingBeforeAndAfterMethodsJUnitTests {
} }
} }
protected static class AlwaysFailingAfterTestClassTestExecutionListener extends AbstractTestExecutionListener { protected static class AlwaysFailingAfterTestClassTestExecutionListener implements TestExecutionListener {
@Override @Override
public void afterTestClass(TestContext testContext) { public void afterTestClass(TestContext testContext) {
@ -109,7 +105,7 @@ public class FailingBeforeAndAfterMethodsJUnitTests {
} }
} }
protected static class AlwaysFailingPrepareTestInstanceTestExecutionListener extends AbstractTestExecutionListener { protected static class AlwaysFailingPrepareTestInstanceTestExecutionListener implements TestExecutionListener {
@Override @Override
public void prepareTestInstance(TestContext testContext) throws Exception { public void prepareTestInstance(TestContext testContext) throws Exception {
@ -117,7 +113,7 @@ public class FailingBeforeAndAfterMethodsJUnitTests {
} }
} }
protected static class AlwaysFailingBeforeTestMethodTestExecutionListener extends AbstractTestExecutionListener { protected static class AlwaysFailingBeforeTestMethodTestExecutionListener implements TestExecutionListener {
@Override @Override
public void beforeTestMethod(TestContext testContext) { public void beforeTestMethod(TestContext testContext) {
@ -125,7 +121,15 @@ public class FailingBeforeAndAfterMethodsJUnitTests {
} }
} }
protected static class AlwaysFailingAfterTestMethodTestExecutionListener extends AbstractTestExecutionListener { protected static class AlwaysFailingBeforeTestExecutionTestExecutionListener implements TestExecutionListener {
@Override
public void beforeTestExecution(TestContext testContext) {
fail("always failing beforeTestExecution()");
}
}
protected static class AlwaysFailingAfterTestMethodTestExecutionListener implements TestExecutionListener {
@Override @Override
public void afterTestMethod(TestContext testContext) { public void afterTestMethod(TestContext testContext) {
@ -133,8 +137,15 @@ public class FailingBeforeAndAfterMethodsJUnitTests {
} }
} }
protected static class AlwaysFailingAfterTestExecutionTestExecutionListener implements TestExecutionListener {
@Override
public void afterTestExecution(TestContext testContext) {
fail("always failing afterTestExecution()");
}
}
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@TestExecutionListeners({})
public static abstract class BaseTestCase { public static abstract class BaseTestCase {
@Test @Test
@ -162,6 +173,16 @@ public class FailingBeforeAndAfterMethodsJUnitTests {
public static class AlwaysFailingBeforeTestMethodTestCase extends BaseTestCase { public static class AlwaysFailingBeforeTestMethodTestCase extends BaseTestCase {
} }
@Ignore("TestCase classes are run manually by the enclosing test class")
@TestExecutionListeners(AlwaysFailingBeforeTestExecutionTestExecutionListener.class)
public static class AlwaysFailingBeforeTestExecutionTestCase extends BaseTestCase {
}
@Ignore("TestCase classes are run manually by the enclosing test class")
@TestExecutionListeners(AlwaysFailingAfterTestExecutionTestExecutionListener.class)
public static class AlwaysFailingAfterTestExecutionTestCase extends BaseTestCase {
}
@Ignore("TestCase classes are run manually by the enclosing test class") @Ignore("TestCase classes are run manually by the enclosing test class")
@TestExecutionListeners(AlwaysFailingAfterTestMethodTestExecutionListener.class) @TestExecutionListeners(AlwaysFailingAfterTestMethodTestExecutionListener.class)
public static class AlwaysFailingAfterTestMethodTestCase extends BaseTestCase { public static class AlwaysFailingAfterTestMethodTestCase extends BaseTestCase {

107
spring-test/src/test/java/org/springframework/test/context/junit4/FailingBeforeAndAfterMethodsTestNGTests.java

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,9 +16,6 @@
package org.springframework.test.context.junit4; package org.springframework.test.context.junit4;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.Parameterized; import org.junit.runners.Parameterized;
@ -28,7 +25,6 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestContext; import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestExecutionListener; import org.springframework.test.context.TestExecutionListener;
import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.support.AbstractTestExecutionListener;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests; import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.springframework.test.context.testng.TrackingTestNGTestListener; import org.springframework.test.context.testng.TrackingTestNGTestListener;
@ -41,25 +37,15 @@ import org.testng.TestNG;
import static org.junit.Assert.*; import static org.junit.Assert.*;
/** /**
* <p> * Integration tests which verify that '<i>before</i>' and '<i>after</i>'
* JUnit 4 based integration test for verifying that '<i>before</i>' and '<i>after</i>'
* methods of {@link TestExecutionListener TestExecutionListeners} as well as * methods of {@link TestExecutionListener TestExecutionListeners} as well as
* {@link BeforeTransaction &#064;BeforeTransaction} and * {@code @BeforeTransaction} and {@code @AfterTransaction} methods can fail
* {@link AfterTransaction &#064;AfterTransaction} methods can fail a test in a * tests in a TestNG environment.
* TestNG environment, as requested in <a *
* href="http://opensource.atlassian.com/projects/spring/browse/SPR-3960" * <p>See: <a href="https://jira.spring.io/browse/SPR-3960" target="_blank">SPR-3960</a>.
* target="_blank">SPR-3960</a>. *
* </p> * <p>Indirectly, this class also verifies that all {@code TestExecutionListener}
* <p>
* Indirectly, this class also verifies that all {@link TestExecutionListener}
* lifecycle callbacks are called. * lifecycle callbacks are called.
* </p>
* <p>
* As of Spring 3.0, this class also tests support for the new
* {@link TestExecutionListener#beforeTestClass(TestContext) beforeTestClass()}
* and {@link TestExecutionListener#afterTestClass(TestContext)
* afterTestClass()} lifecycle callback methods.
* </p>
* *
* @author Sam Brannen * @author Sam Brannen
* @since 2.5 * @since 2.5
@ -75,17 +61,20 @@ public class FailingBeforeAndAfterMethodsTestNGTests {
@Parameters(name = "{0}") @Parameters(name = "{0}")
public static Collection<Object[]> testData() { public static Object[][] testData() {
return Arrays.asList(new Object[][] {// // @formatter:off
// return new Object[][] {
{ AlwaysFailingBeforeTestClassTestCase.class.getSimpleName(), 1, 0, 0, 1 },// { AlwaysFailingBeforeTestClassTestCase.class.getSimpleName(), 1, 0, 0, 1 },
{ AlwaysFailingAfterTestClassTestCase.class.getSimpleName(), 1, 1, 0, 1 },// { AlwaysFailingAfterTestClassTestCase.class.getSimpleName(), 1, 1, 0, 1 },
{ AlwaysFailingPrepareTestInstanceTestCase.class.getSimpleName(), 1, 0, 0, 1 },// { AlwaysFailingPrepareTestInstanceTestCase.class.getSimpleName(), 1, 0, 0, 1 },
{ AlwaysFailingBeforeTestMethodTestCase.class.getSimpleName(), 1, 0, 0, 1 },// { AlwaysFailingBeforeTestMethodTestCase.class.getSimpleName(), 1, 0, 0, 1 },
{ AlwaysFailingAfterTestMethodTestCase.class.getSimpleName(), 1, 1, 0, 1 },// { AlwaysFailingBeforeTestExecutionTestCase.class.getSimpleName(), 1, 0, 1, 0 },
{ FailingBeforeTransactionTestCase.class.getSimpleName(), 1, 0, 0, 1 },// { AlwaysFailingAfterTestExecutionTestCase.class.getSimpleName(), 1, 0, 1, 0 },
{ FailingAfterTransactionTestCase.class.getSimpleName(), 1, 1, 0, 1 } // { AlwaysFailingAfterTestMethodTestCase.class.getSimpleName(), 1, 1, 0, 1 },
}); { FailingBeforeTransactionTestCase.class.getSimpleName(), 1, 0, 0, 1 },
{ FailingAfterTransactionTestCase.class.getSimpleName(), 1, 1, 0, 1 }
};
// @formatter:on
} }
public FailingBeforeAndAfterMethodsTestNGTests(String testClassName, int expectedTestStartCount, public FailingBeforeAndAfterMethodsTestNGTests(String testClassName, int expectedTestStartCount,
@ -106,19 +95,19 @@ public class FailingBeforeAndAfterMethodsTestNGTests {
testNG.setVerbose(0); testNG.setVerbose(0);
testNG.run(); testNG.run();
assertEquals("Verifying number of test starts for test class [" + this.clazz + "].", String name = this.clazz.getSimpleName();
this.expectedTestStartCount, listener.testStartCount);
assertEquals("Verifying number of successful tests for test class [" + this.clazz + "].", assertEquals("tests started for [" + name + "] ==> ", this.expectedTestStartCount, listener.testStartCount);
this.expectedTestSuccessCount, listener.testSuccessCount); assertEquals("successful tests for [" + name + "] ==> ", this.expectedTestSuccessCount,
assertEquals("Verifying number of failures for test class [" + this.clazz + "].", this.expectedFailureCount, listener.testSuccessCount);
listener.testFailureCount); assertEquals("failed tests for [" + name + "] ==> ", this.expectedFailureCount, listener.testFailureCount);
assertEquals("Verifying number of failed configurations for test class [" + this.clazz + "].", assertEquals("failed configurations for [" + name + "] ==> ", this.expectedFailedConfigurationsCount,
this.expectedFailedConfigurationsCount, listener.failedConfigurationsCount); listener.failedConfigurationsCount);
} }
// ------------------------------------------------------------------- // -------------------------------------------------------------------
static class AlwaysFailingBeforeTestClassTestExecutionListener extends AbstractTestExecutionListener { static class AlwaysFailingBeforeTestClassTestExecutionListener implements TestExecutionListener {
@Override @Override
public void beforeTestClass(TestContext testContext) { public void beforeTestClass(TestContext testContext) {
@ -126,7 +115,7 @@ public class FailingBeforeAndAfterMethodsTestNGTests {
} }
} }
static class AlwaysFailingAfterTestClassTestExecutionListener extends AbstractTestExecutionListener { static class AlwaysFailingAfterTestClassTestExecutionListener implements TestExecutionListener {
@Override @Override
public void afterTestClass(TestContext testContext) { public void afterTestClass(TestContext testContext) {
@ -134,7 +123,7 @@ public class FailingBeforeAndAfterMethodsTestNGTests {
} }
} }
static class AlwaysFailingPrepareTestInstanceTestExecutionListener extends AbstractTestExecutionListener { static class AlwaysFailingPrepareTestInstanceTestExecutionListener implements TestExecutionListener {
@Override @Override
public void prepareTestInstance(TestContext testContext) throws Exception { public void prepareTestInstance(TestContext testContext) throws Exception {
@ -142,7 +131,7 @@ public class FailingBeforeAndAfterMethodsTestNGTests {
} }
} }
static class AlwaysFailingBeforeTestMethodTestExecutionListener extends AbstractTestExecutionListener { static class AlwaysFailingBeforeTestMethodTestExecutionListener implements TestExecutionListener {
@Override @Override
public void beforeTestMethod(TestContext testContext) { public void beforeTestMethod(TestContext testContext) {
@ -150,7 +139,23 @@ public class FailingBeforeAndAfterMethodsTestNGTests {
} }
} }
static class AlwaysFailingAfterTestMethodTestExecutionListener extends AbstractTestExecutionListener { static class AlwaysFailingBeforeTestExecutionTestExecutionListener implements TestExecutionListener {
@Override
public void beforeTestExecution(TestContext testContext) {
org.testng.Assert.fail("always failing beforeTestExecution()");
}
}
static class AlwaysFailingAfterTestExecutionTestExecutionListener implements TestExecutionListener {
@Override
public void afterTestExecution(TestContext testContext) {
org.testng.Assert.fail("always failing afterTestExecution()");
}
}
static class AlwaysFailingAfterTestMethodTestExecutionListener implements TestExecutionListener {
@Override @Override
public void afterTestMethod(TestContext testContext) { public void afterTestMethod(TestContext testContext) {
@ -160,7 +165,7 @@ public class FailingBeforeAndAfterMethodsTestNGTests {
// ------------------------------------------------------------------- // -------------------------------------------------------------------
@TestExecutionListeners(value = {}, inheritListeners = false) @TestExecutionListeners(inheritListeners = false)
public static abstract class BaseTestCase extends AbstractTestNGSpringContextTests { public static abstract class BaseTestCase extends AbstractTestNGSpringContextTests {
@org.testng.annotations.Test @org.testng.annotations.Test
@ -184,6 +189,14 @@ public class FailingBeforeAndAfterMethodsTestNGTests {
public static class AlwaysFailingBeforeTestMethodTestCase extends BaseTestCase { public static class AlwaysFailingBeforeTestMethodTestCase extends BaseTestCase {
} }
@TestExecutionListeners(AlwaysFailingBeforeTestExecutionTestExecutionListener.class)
public static class AlwaysFailingBeforeTestExecutionTestCase extends BaseTestCase {
}
@TestExecutionListeners(AlwaysFailingAfterTestExecutionTestExecutionListener.class)
public static class AlwaysFailingAfterTestExecutionTestCase extends BaseTestCase {
}
@TestExecutionListeners(AlwaysFailingAfterTestMethodTestExecutionListener.class) @TestExecutionListeners(AlwaysFailingAfterTestMethodTestExecutionListener.class)
public static class AlwaysFailingAfterTestMethodTestCase extends BaseTestCase { public static class AlwaysFailingAfterTestMethodTestCase extends BaseTestCase {
} }

7
spring-test/src/test/java/org/springframework/test/context/junit4/rules/FailingBeforeAndAfterMethodsSpringRuleTests.java

@ -27,7 +27,7 @@ import org.junit.runners.Parameterized.Parameters;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.FailingBeforeAndAfterMethodsJUnitTests; import org.springframework.test.context.junit4.FailingBeforeAndAfterMethodsSpringRunnerTests;
import org.springframework.test.context.transaction.AfterTransaction; import org.springframework.test.context.transaction.AfterTransaction;
import org.springframework.test.context.transaction.BeforeTransaction; import org.springframework.test.context.transaction.BeforeTransaction;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -35,14 +35,14 @@ import org.springframework.transaction.annotation.Transactional;
import static org.junit.Assert.*; import static org.junit.Assert.*;
/** /**
* This class is an extension of {@link FailingBeforeAndAfterMethodsJUnitTests} * This class is an extension of {@link FailingBeforeAndAfterMethodsSpringRunnerTests}
* that has been modified to use {@link SpringClassRule} and * that has been modified to use {@link SpringClassRule} and
* {@link SpringMethodRule}. * {@link SpringMethodRule}.
* *
* @author Sam Brannen * @author Sam Brannen
* @since 4.2 * @since 4.2
*/ */
public class FailingBeforeAndAfterMethodsSpringRuleTests extends FailingBeforeAndAfterMethodsJUnitTests { public class FailingBeforeAndAfterMethodsSpringRuleTests extends FailingBeforeAndAfterMethodsSpringRunnerTests {
@Parameters(name = "{0}") @Parameters(name = "{0}")
public static Object[] testData() { public static Object[] testData() {
@ -69,7 +69,6 @@ public class FailingBeforeAndAfterMethodsSpringRuleTests extends FailingBeforeAn
// All tests are in superclass. // All tests are in superclass.
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
@TestExecutionListeners({})
public static abstract class BaseSpringRuleTestCase { public static abstract class BaseSpringRuleTestCase {
@ClassRule @ClassRule

Loading…
Cancel
Save