Skip to content

Commit

Permalink
NullPointerException at test with timeOut
Browse files Browse the repository at this point in the history
  • Loading branch information
krmahadevan committed Feb 28, 2020
1 parent beb9d8d commit 78a3ce7
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/testng/internal/InvokeMethodRunnable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/test/hook/issue2251/AbstractBaseTestCase.java
Original file line number Diff line number Diff line change
@@ -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 <T extends Throwable> 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;
}
}
21 changes: 21 additions & 0 deletions src/test/java/test/hook/issue2251/IssueTest.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
19 changes: 19 additions & 0 deletions src/test/java/test/hook/issue2251/SampleTestCase.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
1 change: 1 addition & 0 deletions src/test/resources/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,7 @@
<test name="Hookable" >
<classes>
<class name="test.hook.HookableTest"/>
<class name="test.hook.issue2251.IssueTest"/>
</classes>
</test>

Expand Down

0 comments on commit 78a3ce7

Please sign in to comment.