gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:30:28 24 4
gpt4 key购买 nike

在根据优先级对测试方法进行排序后尝试运行 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)

最佳答案

请将您的拦截器更改为如下所示:

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 注释。

关于java - IMethodInterceptor 在重新排序测试方法时抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46260969/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com