TestContextManager consistently handles Errors from listener invocations

Issue: SPR-13961
This commit is contained in:
Juergen Hoeller 2016-02-18 21:07:32 +01:00
parent 08eb623c41
commit 0adc4921ed
1 changed files with 38 additions and 27 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * 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.
@ -25,6 +25,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
/** /**
* {@code TestContextManager} is the main entry point into the <em>Spring * {@code TestContextManager} is the main entry point into the <em>Spring
@ -194,10 +195,12 @@ public class TestContextManager {
try { try {
testExecutionListener.beforeTestClass(getTestContext()); testExecutionListener.beforeTestClass(getTestContext());
} }
catch (Exception ex) { catch (Throwable ex) {
if (logger.isWarnEnabled()) {
logger.warn("Caught exception while allowing TestExecutionListener [" + testExecutionListener + logger.warn("Caught exception while allowing TestExecutionListener [" + testExecutionListener +
"] to process 'before class' callback for test class [" + testClass + "]", ex); "] to process 'before class' callback for test class [" + testClass + "]", ex);
throw ex; }
ReflectionUtils.rethrowException(ex);
} }
} }
} }
@ -227,10 +230,12 @@ public class TestContextManager {
try { try {
testExecutionListener.prepareTestInstance(getTestContext()); testExecutionListener.prepareTestInstance(getTestContext());
} }
catch (Exception ex) { catch (Throwable ex) {
if (logger.isErrorEnabled()) {
logger.error("Caught exception while allowing TestExecutionListener [" + testExecutionListener + logger.error("Caught exception while allowing TestExecutionListener [" + testExecutionListener +
"] to prepare test instance [" + testInstance + "]", ex); "] to prepare test instance [" + testInstance + "]", ex);
throw ex; }
ReflectionUtils.rethrowException(ex);
} }
} }
} }
@ -264,11 +269,13 @@ public class TestContextManager {
try { try {
testExecutionListener.beforeTestMethod(getTestContext()); testExecutionListener.beforeTestMethod(getTestContext());
} }
catch (Exception ex) { catch (Throwable ex) {
if (logger.isWarnEnabled()) {
logger.warn("Caught exception while allowing TestExecutionListener [" + testExecutionListener + logger.warn("Caught exception while allowing TestExecutionListener [" + testExecutionListener +
"] to process 'before' execution of test method [" + testMethod + "] for test instance [" + "] to process 'before' execution of test method [" + testMethod + "] for test instance [" +
testInstance + "]", ex); testInstance + "]", ex);
throw ex; }
ReflectionUtils.rethrowException(ex);
} }
} }
} }
@ -305,24 +312,26 @@ public class TestContextManager {
} }
getTestContext().updateState(testInstance, testMethod, exception); getTestContext().updateState(testInstance, testMethod, exception);
Exception afterTestMethodException = null; Throwable afterTestMethodException = null;
// Traverse the TestExecutionListeners in reverse order to ensure proper // Traverse the TestExecutionListeners in reverse order to ensure proper
// "wrapper"-style execution of listeners. // "wrapper"-style execution of listeners.
for (TestExecutionListener testExecutionListener : getReversedTestExecutionListeners()) { for (TestExecutionListener testExecutionListener : getReversedTestExecutionListeners()) {
try { try {
testExecutionListener.afterTestMethod(getTestContext()); testExecutionListener.afterTestMethod(getTestContext());
} }
catch (Exception ex) { catch (Throwable ex) {
if (logger.isWarnEnabled()) {
logger.warn("Caught exception while allowing TestExecutionListener [" + testExecutionListener + logger.warn("Caught exception while allowing TestExecutionListener [" + testExecutionListener +
"] to process 'after' execution for test: method [" + testMethod + "], instance [" + "] to process 'after' execution for test: method [" + testMethod + "], instance [" +
testInstance + "], exception [" + exception + "]", ex); testInstance + "], exception [" + exception + "]", ex);
}
if (afterTestMethodException == null) { if (afterTestMethodException == null) {
afterTestMethodException = ex; afterTestMethodException = ex;
} }
} }
} }
if (afterTestMethodException != null) { if (afterTestMethodException != null) {
throw afterTestMethodException; ReflectionUtils.rethrowException(afterTestMethodException);
} }
} }
@ -347,14 +356,15 @@ public class TestContextManager {
} }
getTestContext().updateState(null, null, null); getTestContext().updateState(null, null, null);
Exception afterTestClassException = null; Throwable afterTestClassException = null;
// Traverse the TestExecutionListeners in reverse order to ensure proper // Traverse the TestExecutionListeners in reverse order to ensure proper
// "wrapper"-style execution of listeners. // "wrapper"-style execution of listeners.
for (TestExecutionListener testExecutionListener : getReversedTestExecutionListeners()) { for (TestExecutionListener testExecutionListener : getReversedTestExecutionListeners()) {
try { try {
testExecutionListener.afterTestClass(getTestContext()); testExecutionListener.afterTestClass(getTestContext());
} }
catch (Exception ex) { catch (Throwable ex) {
if (logger.isWarnEnabled()) {
logger.warn("Caught exception while allowing TestExecutionListener [" + testExecutionListener + logger.warn("Caught exception while allowing TestExecutionListener [" + testExecutionListener +
"] to process 'after class' callback for test class [" + testClass + "]", ex); "] to process 'after class' callback for test class [" + testClass + "]", ex);
if (afterTestClassException == null) { if (afterTestClassException == null) {
@ -362,8 +372,9 @@ public class TestContextManager {
} }
} }
} }
}
if (afterTestClassException != null) { if (afterTestClassException != null) {
throw afterTestClassException; ReflectionUtils.rethrowException(afterTestClassException);
} }
} }