Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure ITestContext available for JUnit4 tests #2848

Merged
merged 1 commit into from Dec 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.txt
@@ -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
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
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
@@ -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();
}
}
@@ -0,0 +1,9 @@
package test.junit4.issue2792;

import org.junit.Test;

public class TestClassSample {

@Test
public void testMethod() {}
}
@@ -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());
}
}