testng-team / testng

TestNG testing framework
https://testng.org
Apache License 2.0
1.99k stars 1.02k forks source link

NPE Reporter.getCurrentTestResult() in listener during onTestStart #2224

Closed javydreamercsw closed 4 years ago

javydreamercsw commented 4 years ago

TestNG Version

7.1.1

Expected behavior

Not throw a NPE. Same code worked fine on 7.0.0.

Actual behavior

Throws a NPE

Is the issue reproductible on runner?

Test case sample

public class Example extends TestListenerAdapter {
...
   public void onTestStart(ITestResult tr) {
      Reporter.getCurrentTestResult().getTestContext();
   }
}
krmahadevan commented 4 years ago

@javydreamercsw - The latest released version of TestNG is 7.1.0 and NOT 7.1.1 See here

I am not able to reproduce this in TestNG 7.1.0. Here's a full fledged sample that re-iterates that this is not reproducible. I have tried via surefire plugin as well and its still not reproducible.

Will be happy to re-open if you could please help provide a simple sample test that can be used to recreate this issue.

Closing this issue with resolution as Not reproducible

Test class

import org.testng.Reporter;
import org.testng.annotations.Test;

public class SampleTestClassFor2224 {

  @Test
  public void testMethod() {
    Reporter.log("Running testMethod()", true);
  }
}

Sample listener

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.TestListenerAdapter;

public class LocalListener extends TestListenerAdapter {

  private final List<ITestContext> contexts = new LinkedList<>();
  private final Map<String, List<String>> testMethodLogs = new HashMap<>();

  @Override
  public void onTestStart(ITestResult result) {
    ITestContext ctx = Reporter.getCurrentTestResult().getTestContext();
    ctx.setAttribute("localListener", "Invoked");
    contexts.add(ctx);
  }

  public List<ITestContext> getContexts() {
    return contexts;
  }

  public List<String> getTestMethodLogs(String methodName) {
    return Collections.unmodifiableList(testMethodLogs.get(methodName));
  }

  @Override
  public void onTestSuccess(ITestResult tr) {
    testMethodLogs.put(tr.getMethod().getMethodName(), Reporter.getOutput(tr));
  }
}

Test runner

import java.io.File;
import java.util.Collections;
import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;
import org.testng.TestNG;
import org.testng.annotations.Test;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

public class IssueTest {

  @Test
  public void testIssue() {
    TestNG testNG = new TestNG();
    String location = TestNG.class.getProtectionDomain().getCodeSource().getLocation().getFile();
    String version = new File(location).getParentFile().getName();
    // Ensure we are running on TestNG 7.1.0
    assertThat(version).isEqualToIgnoringCase("7.1.0");
    XmlSuite xmlSuite = new XmlSuite();
    xmlSuite.setName("2224_suite");
    XmlTest xmlTest = new XmlTest(xmlSuite);
    xmlTest.setName("2224_test");
    XmlClass xmlClass = new XmlClass(SampleTestClassFor2224.class.getName());
    xmlTest.setXmlClasses(Collections.singletonList(xmlClass));
    xmlSuite.setVerbose(2);
    LocalListener listener = new LocalListener();
    testNG.addListener(listener);
    testNG.setXmlSuites(Collections.singletonList(xmlSuite));
    System.err.println(xmlSuite.toXml());
    testNG.run();
    // Ensure that we didnt see any failures, which would have happened if there was an NPE
    assertThat(testNG.hasFailure()).isFalse();

    // Ensure that the listener we wired in was invoked and
    // that we were able to retrieve the context as well.
    assertThat(listener.getContexts().size()).isEqualTo(1);
    assertThat(listener.getContexts().get(0).getName()).isEqualTo("2224_test");
    List<String> expected = Collections.singletonList("Running testMethod()");

    // Ensure that we were able to invoke the test method as well.
    assertThat(listener.getTestMethodLogs("testMethod")).containsExactlyElementsOf(expected);
  }
}

Output


...
... TestNG 7.1.0 by Cédric Beust (cedric@beust.com)
...

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite verbose="2" name="2224_suite">
  <test thread-count="5" verbose="2" name="2224_test">
    <classes>
      <class name="com.rationaleemotions.github.issue2224.SampleTestClassFor2224"/>
    </classes>
  </test> <!-- 2224_test -->
</suite> <!-- 2224_suite -->

Running testMethod()
PASSED: testMethod

===============================================
    2224_test
    Tests run: 1, Failures: 0, Skips: 0
===============================================

===============================================
2224_suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

===============================================
Default Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

Process finished with exit code 0