Introduce before/after test execution support in AbstractTestNGSpringContextTests
Issue: SPR-4365
This commit is contained in:
@@ -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");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -49,12 +49,11 @@ import org.testng.annotations.BeforeMethod;
|
||||
* <p>Concrete subclasses:
|
||||
* <ul>
|
||||
* <li>Typically declare a class-level {@link ContextConfiguration
|
||||
* @ContextConfiguration} annotation to configure the {@link ApplicationContext
|
||||
* application context} {@link ContextConfiguration#locations() resource locations}
|
||||
* or {@link ContextConfiguration#classes() annotated classes}. <em>If your test
|
||||
* @ContextConfiguration} annotation to configure the {@linkplain ApplicationContext
|
||||
* application context} {@linkplain ContextConfiguration#locations() resource locations}
|
||||
* or {@linkplain ContextConfiguration#classes() annotated classes}. <em>If your test
|
||||
* does not need to load an application context, you may choose to omit the
|
||||
* {@link ContextConfiguration @ContextConfiguration} declaration and to
|
||||
* configure the appropriate
|
||||
* {@code @ContextConfiguration} declaration and to configure the appropriate
|
||||
* {@link org.springframework.test.context.TestExecutionListener TestExecutionListeners}
|
||||
* manually.</em></li>
|
||||
* <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
|
||||
* the internal {@link TestContextManager} for the current test.
|
||||
* the internal {@link TestContextManager} for the current test class.
|
||||
*/
|
||||
public AbstractTestNGSpringContextTests() {
|
||||
this.testContextManager = new TestContextManager(getClass());
|
||||
@@ -124,7 +123,7 @@ public abstract class AbstractTestNGSpringContextTests implements IHookable, App
|
||||
|
||||
/**
|
||||
* Delegates to the configured {@link TestContextManager} to call
|
||||
* {@link TestContextManager#beforeTestClass() 'before test class'}
|
||||
* {@linkplain TestContextManager#beforeTestClass() 'before test class'}
|
||||
* callbacks.
|
||||
*
|
||||
* @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
|
||||
* {@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
|
||||
* injecting dependencies, etc.
|
||||
*
|
||||
@@ -151,11 +150,11 @@ public abstract class AbstractTestNGSpringContextTests implements IHookable, App
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @param testMethod the test method which is about to be executed.
|
||||
* @throws Exception allows all exceptions to propagate.
|
||||
* @param testMethod the test method which is about to be executed
|
||||
* @throws Exception allows all exceptions to propagate
|
||||
*/
|
||||
@BeforeMethod(alwaysRun = true)
|
||||
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
|
||||
* and then tracks the exception thrown during test execution, if any.
|
||||
*
|
||||
* @see org.testng.IHookable#run(org.testng.IHookCallBack,
|
||||
* org.testng.ITestResult)
|
||||
* @see org.testng.IHookable#run(IHookCallBack, ITestResult)
|
||||
*/
|
||||
@Override
|
||||
public void run(IHookCallBack callBack, ITestResult testResult) {
|
||||
callBack.runTestMethod(testResult);
|
||||
Method testMethod = testResult.getMethod().getConstructorOrMethod().getMethod();
|
||||
boolean beforeCallbacksExecuted = false;
|
||||
|
||||
Throwable testResultException = testResult.getThrowable();
|
||||
if (testResultException instanceof InvocationTargetException) {
|
||||
testResultException = ((InvocationTargetException) testResultException).getCause();
|
||||
try {
|
||||
this.testContextManager.beforeTestExecution(this, testMethod);
|
||||
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
|
||||
* {@link TestContextManager#afterTestMethod(Object, Method, Throwable)
|
||||
* {@linkplain TestContextManager#afterTestMethod(Object, Method, Throwable)
|
||||
* post-process} the test method after the actual test has executed.
|
||||
*
|
||||
* @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
|
||||
* {@link TestContextManager#afterTestClass() 'after test class'} callbacks.
|
||||
* {@linkplain TestContextManager#afterTestClass() 'after test class'} callbacks.
|
||||
*
|
||||
* @throws Exception if a registered TestExecutionListener throws an
|
||||
* exception
|
||||
@@ -212,4 +229,12 @@ public abstract class AbstractTestNGSpringContextTests implements IHookable, App
|
||||
this.testContextManager.afterTestClass();
|
||||
}
|
||||
|
||||
private Throwable getTestResultException(ITestResult testResult) {
|
||||
Throwable testResultException = testResult.getThrowable();
|
||||
if (testResultException instanceof InvocationTargetException) {
|
||||
testResultException = ((InvocationTargetException) testResultException).getCause();
|
||||
}
|
||||
return testResultException;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user