2017-09-17 51 views
1

我得到NullPointerException异常,而试图订购基于优先级的测试方法后运行TestNG的测试。我试图给测试方法添加优先级,但没有帮助。有人能帮我解决这个问题吗?我是TestNG听众的新手。IMethodInterceptor抛出NullPointerException异常而重新排序测试方法

以下是我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) 
+2

可能重复[什么是NullPointerException,以及如何解决它?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-doi-i-fix -it) –

回答

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注解。但是,当你查询方法的注释,它回来为空,因为这种方法不会对任何@Test注解,但其具有该注释封闭类。

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

或者你可以摆脱你已经在类级别添加@Test注释。

+0

谢谢!按照您的建议修改拦截器后,它工作。 – Harish

相关问题