IMethodInterceptor在重新排序测试方法时抛出NullPointerException

时间:2017-09-17 05:23:18

标签: java nullpointerexception testng

在根据优先级排序测试方法后尝试运行testng测试时,我得到NullPointerException。我尝试在测试方法中添加优先级,但没有帮助。有人可以帮我解决这个问题。我是TestNG听众的新手。

以下是我的IMEthodInterceptor实现:

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;
}

}

以下是测试类:

@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);
}

}

以下是testng.xml内容:

<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 -->

以下是我尝试使用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)

1 个答案:

答案 0 :(得分:1)

请将您的拦截器更改为以下内容:

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;
}

NPE背后的原因是TestNG也在提供数据提供者注释方法

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

到你的方法拦截器,因为你有一个类级别@Test注释。但是当你查询方法的注释时,它会返回null,因为这个方法上没有任何@Test注释,但它是带有这个注释的封闭类。

修复基本上是添加额外的空检查。

或者,您可以删除在课程级别添加的@Test注释。