1

I get NullPointerException while trying to run testng tests after ordering the test methods based on their priorities. I tried adding priorities to the test methods but not helpful. Could someone please help me in resolving this. I'm new to TestNG listeners.

Following is my IMEthodInterceptor implementation:

public class TestNGImethodInterceptor implements IMethodInterceptor {

@Override
public List<IMethodInstance> intercept (List<IMethodInstance> methods, ITestContext context){
    List<IMethodInstance> sortedMethods = new ArrayList<IMethodInstance>();
    for(IMethodInstance method : methods){
        Test testMethod = method.getMethod().getConstructorOrMethod().getMethod().getAnnotation(Test.class);
        if(testMethod.priority() == 1){
            sortedMethods.add(method);
        }
    }
    return sortedMethods;
}

}

Following is the test class:

@Test(groups = {"functest"})
public class TestNGAnnotations {

@BeforeSuite
public void beforeSuite(){
    System.out.println("Before Suite in Super Class");
}

@AfterSuite
public void afterSuite(){
    System.out.println("After Suite in Super Class");
}

@BeforeTest
public void beforeTest(){
    System.out.println("Before Test in Super Class");
}

@AfterTest
public void afterTest(){
    System.out.println("After Test in Super Class");
}

@BeforeClass
public void beforeClass(){
    System.out.println("Before Class in Super Class");
}

@AfterClass
public void afterClass(){
    System.out.println("After Class in Super Class");
}

@BeforeGroups
public void beforeGroups(){
    System.out.println("Before Groups in Super Class");
}

@AfterGroups
public void afterGroups(){
    System.out.println("After Groups in Super Class");
}

@BeforeMethod(alwaysRun=true)
public void beforeMethod(){
    System.out.println("Before Methods in Super Class");
}

@AfterMethod(alwaysRun=true)
public void afterMethod(){
    System.out.println("After Methods in Super Class");
}

@Test(groups = {"functest", "checkintest"})
public void test1(){
    System.out.println("Test method 1 in super class");
}

@Test(groups = {"functest", "checkintest"})
public String test2(){
    System.out.println("Test method 2 in super class");
    return "Hello";
}

@Test(groups = {"functest"})
public void test3(){
    System.out.println("Test method 3 in super class");
}

@Test(priority=1,groups = {"checkintest"})
public String test4(){
    System.out.println("Test method 4 in super class");
    return "Hello";
}

@Parameters("param")
@Test
public void paramTest(String param){
    System.out.println("Parameter received " + param);
}

@DataProvider(name = "test1")
public Object[][] createData1() {
 return new Object[][] {
   { "Cedric", new Integer(36) },
   { "Anne", new Integer(37)},
 };
}

//This test method declares that its data should be supplied by the Data Provider
//named "test1"
@Test(dataProvider = "test1")
public void verifyData1(String n1, Integer n2) {
 System.out.println(n1 + " " + n2);
}

@Test(dataProvider = "create", dataProviderClass=SecondTestNGAnnotation.class)
public void dpFromOtherClass(Integer i){
    System.out.println("DP from other class " + i);
}

}

Following is the testng.xml content:

<suite name="Test Suite" allow-return-values="true">
<parameter name="param" value="Test ParameterSuite"/>
<listeners>
    <listener class-name="com.harish.testng.TestNGImethodInterceptor"   />
</listeners>
<test name="Test">
<parameter name="param" value="Test Parameter"/>
    <groups>
        <run>
            <exclude name="checkintest" />
            <include name="functest" />
        </run>
    </groups>
    <classes>
        <class name="com.harish.testng.TestNGAnnotations" />
    </classes>
</test> <!-- Test -->

Following is the result while I try to run the tests by using testng.xml:

Before Suite in Super Class
Before Test in Super Class
After Test in Super Class
java.lang.NullPointerException
at  com.harish.testng.TestNGImethodInterceptor.intercept(TestNGImethodInterceptor.java:18)
at org.testng.TestRunner.intercept(TestRunner.java:794)
at org.testng.TestRunner.privateRun(TestRunner.java:747)
at org.testng.TestRunner.run(TestRunner.java:634)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:425)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:420)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:385)
at org.testng.SuiteRunner.run(SuiteRunner.java:334)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1318)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1243)
at org.testng.TestNG.runSuites(TestNG.java:1161)
at org.testng.TestNG.run(TestNG.java:1129)
at   org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Harish
  • 226
  • 4
  • 14
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Oleg Estekhin Sep 17 '17 at 08:31

1 Answers1

1

Please change your interceptor to something like below :

public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
    List<IMethodInstance> sortedMethods = new ArrayList<IMethodInstance>();
    for (IMethodInstance method : methods) {
        Test testMethod = method.getMethod().getConstructorOrMethod().getMethod().getAnnotation(Test.class);
        //testMethod would be "null" for "@DataProvider" annotated methods
        if (testMethod != null && testMethod.priority() == 1) {
            sortedMethods.add(method);
        }
    }
    return sortedMethods;
}

The reason behind the NPE is that TestNG is feeding in the data provider annotated method as well

@DataProvider(name = "test1")
public Object[][] createData1() {
    return new Object[][]{
            {"Cedric", new Integer(36)},
            {"Anne", new Integer(37)},
    };
}

to your method interceptor, because you have a class level @Test annotation. But when you query the method's annotation, its comes back as null, because this method doesn't have any @Test annotations on it, but its the enclosing class that has this annotation.

The fix is basically to add an additional null check.

Alternatively you can get rid of the @Test annotation that you have added at the class level.

Krishnan Mahadevan
  • 12,592
  • 5
  • 31
  • 59