Skip to content

Commit 8630a7e

Browse files
committed
Ensure ITestContext available for JUnit4 tests
Closes #2792
1 parent 7070b02 commit 8630a7e

File tree

6 files changed

+71
-4
lines changed

6 files changed

+71
-4
lines changed

CHANGES.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Current
2+
Fixed: GITHUB-2792: JUnitTestClass sets XmlTest as null when running JUnit 4 Tests using TestNG (Krishnan Mahadevan)
23
Fixed: GITHUB-2844: Deprecate support for running Spock Tests (Krishnan Mahadevan)
34
Fixed: GITHUB-550: Weird @BeforeMethod and @AfterMethod behaviour with dependsOnMethods (Krishnan Mahadevan)
45
Fixed: GITHUB-893: TestNG should provide an Api which allow to find all dependent of a specific test (Krishnan Mahadevan)

testng-core/src/main/java/org/testng/junit/JUnit4TestRunner.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,11 @@ private ITestResult createTestResult(ITestObjectFactory objectFactory, Descripti
231231
JUnit4TestClass tc = new JUnit4TestClass(test);
232232
JUnitTestMethod tm = new JUnit4TestMethod(objectFactory, tc, test);
233233

234-
TestResult tr = TestResult.newTestResultFor(tm);
234+
ITestContext ctx = null;
235+
if (m_parentRunner instanceof ITestContext) {
236+
ctx = (ITestContext) m_parentRunner;
237+
}
238+
TestResult tr = TestResult.newContextAwareTestResult(tm, ctx);
235239

236240
InvokedMethod im = new InvokedMethod(tr.getStartMillis(), tr);
237241
if (tr.getMethod() instanceof IInvocationStatus) {

testng-core/src/main/java/org/testng/junit/JUnitTestRunner.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public class JUnitTestRunner implements TestListener, IJUnitTestRunner {
2727
private final ITestObjectFactory m_objectFactory;
2828
private final ITestResultNotifier m_parentRunner;
2929

30-
private Map<Test, TestRunInfo> m_tests = new WeakHashMap<>();
31-
private List<ITestNGMethod> m_methods = Lists.newArrayList();
30+
private final Map<Test, TestRunInfo> m_tests = new WeakHashMap<>();
31+
private final List<ITestNGMethod> m_methods = Lists.newArrayList();
3232
private Collection<IInvokedMethodListener> m_invokedMethodListeners = Lists.newArrayList();
3333

3434
public JUnitTestRunner(ITestObjectFactory objectFactory, ITestResultNotifier tr) {
@@ -102,9 +102,14 @@ private org.testng.internal.TestResult recordResults(Test test, TestRunInfo tri)
102102
JUnitTestClass tc = new JUnit3TestClass(test);
103103
JUnitTestMethod tm = new JUnit3TestMethod(m_objectFactory, tc, test);
104104

105+
ITestContext ctx = null;
106+
if (m_parentRunner instanceof ITestContext) {
107+
ctx = (ITestContext) m_parentRunner;
108+
}
109+
105110
org.testng.internal.TestResult tr =
106111
org.testng.internal.TestResult.newEndTimeAwareTestResult(
107-
tm, null, tri.m_failure, tri.m_start);
112+
tm, ctx, tri.m_failure, tri.m_start);
108113

109114
if (tri.isFailure()) {
110115
tr.setStatus(ITestResult.FAILURE);

testng-core/src/test/java/test/JUnit4Test.java

+17
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package test;
22

3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.testng.TestNG;
36
import org.testng.annotations.BeforeMethod;
47
import org.testng.annotations.DataProvider;
58
import org.testng.annotations.Test;
69
import test.junit4.*;
10+
import test.junit4.issue2792.TestClassSample;
11+
import test.junit4.issue2792.TestContextGatheringListener;
712

813
public class JUnit4Test extends BaseTest {
914

@@ -72,4 +77,16 @@ public void testTests(
7277
verifyFailedTests(expectedFailedTests);
7378
verifySkippedTests(expectedSkippedTests);
7479
}
80+
81+
@Test(description = "GITHUB-2792")
82+
public void ensureTestContextAvailableForListeners() {
83+
TestNG testng = new TestNG();
84+
testng.setTestClasses(new Class[] {TestClassSample.class});
85+
TestContextGatheringListener listener = new TestContextGatheringListener();
86+
testng.addListener(listener);
87+
testng.setJUnit(true);
88+
testng.run();
89+
assertThat(listener.isTestContextFoundOnTestStart()).isTrue();
90+
assertThat(listener.isTestContextFoundOnAfterInvocation()).isTrue();
91+
}
7592
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package test.junit4.issue2792;
2+
3+
import org.junit.Test;
4+
5+
public class TestClassSample {
6+
7+
@Test
8+
public void testMethod() {}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package test.junit4.issue2792;
2+
3+
import java.util.Objects;
4+
import org.testng.IInvokedMethod;
5+
import org.testng.IInvokedMethodListener;
6+
import org.testng.ITestListener;
7+
import org.testng.ITestResult;
8+
9+
public class TestContextGatheringListener implements IInvokedMethodListener, ITestListener {
10+
11+
private boolean testContextFoundOnTestStart = false;
12+
private boolean testContextFoundOnAfterInvocation = false;
13+
14+
public boolean isTestContextFoundOnAfterInvocation() {
15+
return testContextFoundOnAfterInvocation;
16+
}
17+
18+
public boolean isTestContextFoundOnTestStart() {
19+
return testContextFoundOnTestStart;
20+
}
21+
22+
@Override
23+
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
24+
testContextFoundOnAfterInvocation = Objects.nonNull(testResult.getTestContext());
25+
}
26+
27+
@Override
28+
public void onTestStart(ITestResult result) {
29+
testContextFoundOnTestStart = Objects.nonNull(result.getTestContext());
30+
}
31+
}

0 commit comments

Comments
 (0)