|
|
@ -94,13 +94,8 @@ public class TestContextManager { |
|
|
|
|
|
|
|
|
|
|
|
private final TestContext testContext; |
|
|
|
private final TestContext testContext; |
|
|
|
|
|
|
|
|
|
|
|
private final ThreadLocal<TestContext> testContextHolder = new ThreadLocal<TestContext>() { |
|
|
|
private final ThreadLocal<TestContext> testContextHolder = |
|
|
|
|
|
|
|
ThreadLocal.withInitial(() -> copyTestContext(TestContextManager.this.testContext)); |
|
|
|
@Override |
|
|
|
|
|
|
|
protected TestContext initialValue() { |
|
|
|
|
|
|
|
return copyTestContext(TestContextManager.this.testContext); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final List<TestExecutionListener> testExecutionListeners = new ArrayList<>(); |
|
|
|
private final List<TestExecutionListener> testExecutionListeners = new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
|
@ -364,11 +359,13 @@ public class TestContextManager { |
|
|
|
* @see #getTestExecutionListeners() |
|
|
|
* @see #getTestExecutionListeners() |
|
|
|
* @see Throwable#addSuppressed(Throwable) |
|
|
|
* @see Throwable#addSuppressed(Throwable) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void afterTestExecution(Object testInstance, Method testMethod, @Nullable Throwable exception) throws Exception { |
|
|
|
public void afterTestExecution(Object testInstance, Method testMethod, @Nullable Throwable exception) |
|
|
|
|
|
|
|
throws Exception { |
|
|
|
|
|
|
|
|
|
|
|
String callbackName = "afterTestExecution"; |
|
|
|
String callbackName = "afterTestExecution"; |
|
|
|
prepareForAfterCallback(callbackName, testInstance, testMethod, exception); |
|
|
|
prepareForAfterCallback(callbackName, testInstance, testMethod, exception); |
|
|
|
|
|
|
|
|
|
|
|
Throwable afterTestExecutionException = null; |
|
|
|
Throwable afterTestExecutionException = 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()) { |
|
|
@ -385,6 +382,7 @@ public class TestContextManager { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (afterTestExecutionException != null) { |
|
|
|
if (afterTestExecutionException != null) { |
|
|
|
ReflectionUtils.rethrowException(afterTestExecutionException); |
|
|
|
ReflectionUtils.rethrowException(afterTestExecutionException); |
|
|
|
} |
|
|
|
} |
|
|
@ -414,9 +412,8 @@ public class TestContextManager { |
|
|
|
* @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 |
|
|
|
* @param exception the exception that was thrown during execution of the |
|
|
|
* @param exception the exception that was thrown during execution of the test |
|
|
|
* test method or by a TestExecutionListener, or {@code null} if none |
|
|
|
* 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 |
|
|
|
* @see #beforeTestMethod |
|
|
|
* @see #beforeTestMethod |
|
|
|
* @see #beforeTestExecution |
|
|
|
* @see #beforeTestExecution |
|
|
@ -424,11 +421,13 @@ public class TestContextManager { |
|
|
|
* @see #getTestExecutionListeners() |
|
|
|
* @see #getTestExecutionListeners() |
|
|
|
* @see Throwable#addSuppressed(Throwable) |
|
|
|
* @see Throwable#addSuppressed(Throwable) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void afterTestMethod(Object testInstance, Method testMethod, @Nullable Throwable exception) throws Exception { |
|
|
|
public void afterTestMethod(Object testInstance, Method testMethod, @Nullable Throwable exception) |
|
|
|
|
|
|
|
throws Exception { |
|
|
|
|
|
|
|
|
|
|
|
String callbackName = "afterTestMethod"; |
|
|
|
String callbackName = "afterTestMethod"; |
|
|
|
prepareForAfterCallback(callbackName, testInstance, testMethod, exception); |
|
|
|
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
|
|
|
|
// "wrapper"-style execution of listeners.
|
|
|
|
// "wrapper"-style execution of listeners.
|
|
|
|
for (TestExecutionListener testExecutionListener : getReversedTestExecutionListeners()) { |
|
|
|
for (TestExecutionListener testExecutionListener : getReversedTestExecutionListeners()) { |
|
|
@ -445,6 +444,7 @@ public class TestContextManager { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (afterTestMethodException != null) { |
|
|
|
if (afterTestMethodException != null) { |
|
|
|
ReflectionUtils.rethrowException(afterTestMethodException); |
|
|
|
ReflectionUtils.rethrowException(afterTestMethodException); |
|
|
|
} |
|
|
|
} |
|
|
@ -510,33 +510,36 @@ public class TestContextManager { |
|
|
|
@Nullable Throwable exception) { |
|
|
|
@Nullable Throwable exception) { |
|
|
|
|
|
|
|
|
|
|
|
if (logger.isTraceEnabled()) { |
|
|
|
if (logger.isTraceEnabled()) { |
|
|
|
logger.trace(String.format("%s(): instance [%s], method [%s], exception [%s]", callbackName, testInstance, |
|
|
|
logger.trace(String.format("%s(): instance [%s], method [%s], exception [%s]", |
|
|
|
testMethod, exception)); |
|
|
|
callbackName, testInstance, testMethod, exception)); |
|
|
|
} |
|
|
|
} |
|
|
|
getTestContext().updateState(testInstance, testMethod, exception); |
|
|
|
getTestContext().updateState(testInstance, testMethod, exception); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void handleBeforeException(Throwable ex, String callbackName, TestExecutionListener testExecutionListener, |
|
|
|
private void handleBeforeException(Throwable ex, String callbackName, TestExecutionListener testExecutionListener, |
|
|
|
Object testInstance, Method testMethod) throws Exception { |
|
|
|
Object testInstance, Method testMethod) throws Exception { |
|
|
|
|
|
|
|
|
|
|
|
logException(ex, callbackName, testExecutionListener, testInstance, testMethod); |
|
|
|
logException(ex, callbackName, testExecutionListener, testInstance, testMethod); |
|
|
|
ReflectionUtils.rethrowException(ex); |
|
|
|
ReflectionUtils.rethrowException(ex); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void logException(Throwable ex, String callbackName, TestExecutionListener testExecutionListener, |
|
|
|
private void logException( |
|
|
|
Class<?> testClass) { |
|
|
|
Throwable ex, String callbackName, TestExecutionListener testExecutionListener, Class<?> testClass) { |
|
|
|
|
|
|
|
|
|
|
|
if (logger.isWarnEnabled()) { |
|
|
|
if (logger.isWarnEnabled()) { |
|
|
|
logger.warn(String.format("Caught exception while invoking '%s' callback on " + |
|
|
|
logger.warn(String.format("Caught exception while invoking '%s' callback on " + |
|
|
|
"TestExecutionListener [%s] for test class [%s]", callbackName, testExecutionListener, |
|
|
|
"TestExecutionListener [%s] for test class [%s]", callbackName, testExecutionListener, |
|
|
|
testClass), ex); |
|
|
|
testClass), ex); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void logException(Throwable ex, String callbackName, TestExecutionListener testExecutionListener, |
|
|
|
private void logException(Throwable ex, String callbackName, TestExecutionListener testExecutionListener, |
|
|
|
Object testInstance, Method testMethod) { |
|
|
|
Object testInstance, Method testMethod) { |
|
|
|
|
|
|
|
|
|
|
|
if (logger.isWarnEnabled()) { |
|
|
|
if (logger.isWarnEnabled()) { |
|
|
|
logger.warn(String.format("Caught exception while invoking '%s' callback on " + |
|
|
|
logger.warn(String.format("Caught exception while invoking '%s' callback on " + |
|
|
|
"TestExecutionListener [%s] for test method [%s] and test instance [%s]", |
|
|
|
"TestExecutionListener [%s] for test method [%s] and test instance [%s]", |
|
|
|
callbackName, testExecutionListener, testMethod, testInstance), ex); |
|
|
|
callbackName, testExecutionListener, testMethod, testInstance), ex); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -546,8 +549,8 @@ public class TestContextManager { |
|
|
|
* <em>copy constructor</em>. |
|
|
|
* <em>copy constructor</em>. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private static TestContext copyTestContext(TestContext testContext) { |
|
|
|
private static TestContext copyTestContext(TestContext testContext) { |
|
|
|
Constructor<? extends TestContext> constructor = ClassUtils.getConstructorIfAvailable(testContext.getClass(), |
|
|
|
Constructor<? extends TestContext> constructor = |
|
|
|
testContext.getClass()); |
|
|
|
ClassUtils.getConstructorIfAvailable(testContext.getClass(), testContext.getClass()); |
|
|
|
|
|
|
|
|
|
|
|
if (constructor != null) { |
|
|
|
if (constructor != null) { |
|
|
|
try { |
|
|
|
try { |
|
|
@ -563,7 +566,7 @@ public class TestContextManager { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// fallback to original instance
|
|
|
|
// Fallback to original instance
|
|
|
|
return testContext; |
|
|
|
return testContext; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|