Skip to content

Commit

Permalink
Ensure ITestContext available for JUnit4 tests
Browse files Browse the repository at this point in the history
Closes #2792
  • Loading branch information
krmahadevan committed Dec 4, 2022
1 parent 7070b02 commit 8630a7e
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 4 deletions.
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-2792: JUnitTestClass sets XmlTest as null when running JUnit 4 Tests using TestNG (Krishnan Mahadevan)
Fixed: GITHUB-2844: Deprecate support for running Spock Tests (Krishnan Mahadevan)
Fixed: GITHUB-550: Weird @BeforeMethod and @AfterMethod behaviour with dependsOnMethods (Krishnan Mahadevan)
Fixed: GITHUB-893: TestNG should provide an Api which allow to find all dependent of a specific test (Krishnan Mahadevan)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,11 @@ private ITestResult createTestResult(ITestObjectFactory objectFactory, Descripti
JUnit4TestClass tc = new JUnit4TestClass(test);
JUnitTestMethod tm = new JUnit4TestMethod(objectFactory, tc, test);

TestResult tr = TestResult.newTestResultFor(tm);
ITestContext ctx = null;
if (m_parentRunner instanceof ITestContext) {
ctx = (ITestContext) m_parentRunner;
}
TestResult tr = TestResult.newContextAwareTestResult(tm, ctx);

InvokedMethod im = new InvokedMethod(tr.getStartMillis(), tr);
if (tr.getMethod() instanceof IInvocationStatus) {
Expand Down
11 changes: 8 additions & 3 deletions testng-core/src/main/java/org/testng/junit/JUnitTestRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public class JUnitTestRunner implements TestListener, IJUnitTestRunner {
private final ITestObjectFactory m_objectFactory;
private final ITestResultNotifier m_parentRunner;

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

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

ITestContext ctx = null;
if (m_parentRunner instanceof ITestContext) {
ctx = (ITestContext) m_parentRunner;
}

org.testng.internal.TestResult tr =
org.testng.internal.TestResult.newEndTimeAwareTestResult(
tm, null, tri.m_failure, tri.m_start);
tm, ctx, tri.m_failure, tri.m_start);

if (tri.isFailure()) {
tr.setStatus(ITestResult.FAILURE);
Expand Down
17 changes: 17 additions & 0 deletions testng-core/src/test/java/test/JUnit4Test.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package test;

import static org.assertj.core.api.Assertions.assertThat;

import org.testng.TestNG;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import test.junit4.*;
import test.junit4.issue2792.TestClassSample;
import test.junit4.issue2792.TestContextGatheringListener;

public class JUnit4Test extends BaseTest {

Expand Down Expand Up @@ -72,4 +77,16 @@ public void testTests(
verifyFailedTests(expectedFailedTests);
verifySkippedTests(expectedSkippedTests);
}

@Test(description = "GITHUB-2792")
public void ensureTestContextAvailableForListeners() {
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] {TestClassSample.class});
TestContextGatheringListener listener = new TestContextGatheringListener();
testng.addListener(listener);
testng.setJUnit(true);
testng.run();
assertThat(listener.isTestContextFoundOnTestStart()).isTrue();
assertThat(listener.isTestContextFoundOnAfterInvocation()).isTrue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package test.junit4.issue2792;

import org.junit.Test;

public class TestClassSample {

@Test
public void testMethod() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package test.junit4.issue2792;

import java.util.Objects;
import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class TestContextGatheringListener implements IInvokedMethodListener, ITestListener {

private boolean testContextFoundOnTestStart = false;
private boolean testContextFoundOnAfterInvocation = false;

public boolean isTestContextFoundOnAfterInvocation() {
return testContextFoundOnAfterInvocation;
}

public boolean isTestContextFoundOnTestStart() {
return testContextFoundOnTestStart;
}

@Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
testContextFoundOnAfterInvocation = Objects.nonNull(testResult.getTestContext());
}

@Override
public void onTestStart(ITestResult result) {
testContextFoundOnTestStart = Objects.nonNull(result.getTestContext());
}
}

0 comments on commit 8630a7e

Please sign in to comment.