Browse Source

TestContextManager consistently handles Errors from listener invocations

Issue: SPR-13961
pull/973/head
Juergen Hoeller 10 years ago
parent
commit
0adc4921ed
  1. 65
      spring-test/src/main/java/org/springframework/test/context/TestContextManager.java

65
spring-test/src/main/java/org/springframework/test/context/TestContextManager.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.
@ -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) {
logger.warn("Caught exception while allowing TestExecutionListener [" + testExecutionListener + if (logger.isWarnEnabled()) {
"] to process 'before class' callback for test class [" + testClass + "]", ex); logger.warn("Caught exception while allowing TestExecutionListener [" + testExecutionListener +
throw ex; "] to process 'before class' callback for test class [" + testClass + "]", 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) {
logger.error("Caught exception while allowing TestExecutionListener [" + testExecutionListener + if (logger.isErrorEnabled()) {
"] to prepare test instance [" + testInstance + "]", ex); logger.error("Caught exception while allowing TestExecutionListener [" + testExecutionListener +
throw ex; "] to prepare test instance [" + testInstance + "]", 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) {
logger.warn("Caught exception while allowing TestExecutionListener [" + testExecutionListener + if (logger.isWarnEnabled()) {
"] to process 'before' execution of test method [" + testMethod + "] for test instance [" + logger.warn("Caught exception while allowing TestExecutionListener [" + testExecutionListener +
testInstance + "]", ex); "] to process 'before' execution of test method [" + testMethod + "] for test instance [" +
throw ex; testInstance + "]", 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) {
logger.warn("Caught exception while allowing TestExecutionListener [" + testExecutionListener + if (logger.isWarnEnabled()) {
"] to process 'after' execution for test: method [" + testMethod + "], instance [" + logger.warn("Caught exception while allowing TestExecutionListener [" + testExecutionListener +
testInstance + "], exception [" + exception + "]", ex); "] to process 'after' execution for test: method [" + testMethod + "], instance [" +
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,23 +356,25 @@ 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) {
logger.warn("Caught exception while allowing TestExecutionListener [" + testExecutionListener + if (logger.isWarnEnabled()) {
"] to process 'after class' callback for test class [" + testClass + "]", ex); logger.warn("Caught exception while allowing TestExecutionListener [" + testExecutionListener +
if (afterTestClassException == null) { "] to process 'after class' callback for test class [" + testClass + "]", ex);
afterTestClassException = ex; if (afterTestClassException == null) {
afterTestClassException = ex;
}
} }
} }
} }
if (afterTestClassException != null) { if (afterTestClassException != null) {
throw afterTestClassException; ReflectionUtils.rethrowException(afterTestClassException);
} }
} }

Loading…
Cancel
Save