forked from testng-team/testng
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NullPointerException at test with timeOut
Closes testng-team#2251
- Loading branch information
1 parent
beb9d8d
commit 78a3ce7
Showing
6 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/test/java/test/hook/issue2251/AbstractBaseTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters