2017-09-17 52 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

相關問題