diff --git a/CHANGES.txt b/CHANGES.txt index 118902ce63..02b31688a6 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,4 +1,5 @@ Current +Fixed: GITHUB-2251: NullPointerException at test with timeOut (Krishnan Mahadevan) Fixed: GITHUB-2249: Not abstract super-classes mess up test run order (Sergii Kim) Fixed: GITHUB-2195: NPE Using groups and @Before/@AfterMethod with alwaysRun and dependsOnMethods (Tomas & Julien Herr) Fixed: GITHUB-2238: Parameter values should be overridable from JVM arguments (Krishnan Mahadevan) diff --git a/src/main/java/org/testng/internal/InvokeMethodRunnable.java b/src/main/java/org/testng/internal/InvokeMethodRunnable.java index a6063b3579..c6b6e9b422 100644 --- a/src/main/java/org/testng/internal/InvokeMethodRunnable.java +++ b/src/main/java/org/testng/internal/InvokeMethodRunnable.java @@ -50,7 +50,11 @@ private void runOne() { m_instance, m_parameters, m_hookable, m.getMethod(), m_testResult); } } catch (Throwable e) { - t = new TestNGRuntimeException(e.getCause()); + Throwable cause = e.getCause(); + if (cause == null) { + cause = e; + } + t = new TestNGRuntimeException(cause); } if (null != t) { Thread.currentThread().interrupt(); diff --git a/src/test/java/test/hook/issue2251/AbstractBaseTestCase.java b/src/test/java/test/hook/issue2251/AbstractBaseTestCase.java new file mode 100644 index 0000000000..b7174b2f22 --- /dev/null +++ b/src/test/java/test/hook/issue2251/AbstractBaseTestCase.java @@ -0,0 +1,42 @@ +package test.hook.issue2251; + +import java.lang.reflect.InvocationTargetException; +import org.testng.IHookCallBack; +import org.testng.IHookable; +import org.testng.ITestResult; + +/** + * This test class apes on a bare essential What Spring TestNG provides as a base class in terms of + * running Spring based tests. The following methods have been duplicated from + * org.springframework.test.context.testng.AbstractTestNGSpringContextTests to simulate the bug. 1. + * throwAsUncheckedException() 2. getTestResultException() 3. throwAs() + */ +public class AbstractBaseTestCase implements IHookable { + + @Override + public void run(IHookCallBack callBack, ITestResult testResult) { + callBack.runTestMethod(testResult); + Throwable t = getTestResultException(testResult); + if (t != null) { + throwAsUncheckedException(t); + } + } + + @SuppressWarnings("unchecked") + private void throwAs(Throwable t) throws T { + throw (T) t; + } + + private void throwAsUncheckedException(Throwable t) { + throwAs(t); + } + + + private Throwable getTestResultException(ITestResult testResult) { + Throwable testResultException = testResult.getThrowable(); + if (testResultException instanceof InvocationTargetException) { + testResultException = testResultException.getCause(); + } + return testResultException; + } +} diff --git a/src/test/java/test/hook/issue2251/IssueTest.java b/src/test/java/test/hook/issue2251/IssueTest.java new file mode 100644 index 0000000000..13151f841e --- /dev/null +++ b/src/test/java/test/hook/issue2251/IssueTest.java @@ -0,0 +1,21 @@ +package test.hook.issue2251; + +import org.assertj.core.api.Assertions; +import org.testng.TestListenerAdapter; +import org.testng.TestNG; +import org.testng.annotations.Test; +import test.SimpleBaseTest; + +public class IssueTest extends SimpleBaseTest { + + @Test(description = "GITHUB-2251") + public void runTest() { + TestNG testng = create(SampleTestCase.class); + TestListenerAdapter l = new TestListenerAdapter(); + testng.addListener(l); + testng.run(); + Throwable t = l.getFailedTests().get(0).getThrowable(); + Assertions.assertThat(t).isInstanceOf(NullPointerException.class); + } + +} diff --git a/src/test/java/test/hook/issue2251/SampleTestCase.java b/src/test/java/test/hook/issue2251/SampleTestCase.java new file mode 100644 index 0000000000..4d36ac89a9 --- /dev/null +++ b/src/test/java/test/hook/issue2251/SampleTestCase.java @@ -0,0 +1,19 @@ +package test.hook.issue2251; + +import org.testng.annotations.Test; + +public class SampleTestCase extends AbstractBaseTestCase { + + static class NullExObj { + + @Override + public String toString() { + throw new NullPointerException("expected"); + } + } + + @Test(timeOut = 1000000) //removing timeout fixes error output + public void testError() throws Exception { + new NullExObj().toString(); + } +} diff --git a/src/test/resources/testng.xml b/src/test/resources/testng.xml index 6aa96653b2..7fe8807abd 100644 --- a/src/test/resources/testng.xml +++ b/src/test/resources/testng.xml @@ -627,6 +627,7 @@ +