Java testNGCucumberRunner.Providers中存在空指针异常

Java testNGCucumberRunner.Providers中存在空指针异常,java,cucumber,testng,Java,Cucumber,Testng,我在testNGCucumberRunner.providefeactures()中得到一个NullPointerException 我试过调试,它显示testNGCucumberRunner有空值。我试着用黄瓜和TestNG。 在这里发布我的代码和例外。我尝试将testrunner类放在不同的包中,并将代码放在testrunner以外的其他类中。也尝试过getScenarios [RemoteTestNG] detected TestNG version 7.0.0 [Utils] [ERRO

我在
testNGCucumberRunner.providefeactures()中得到一个NullPointerException

我试过调试,它显示testNGCucumberRunner有空值。我试着用黄瓜和TestNG。 在这里发布我的代码和例外。我尝试将testrunner类放在不同的包中,并将代码放在testrunner以外的其他类中。也尝试过getScenarios

[RemoteTestNG] detected TestNG version 7.0.0
[Utils] [ERROR] [Error] java.lang.NullPointerException
    at com.qa.test.testRunner.getFeatures(testRunner.java:40)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:77)
    at org.testng.internal.MethodInvocationHelper.invokeMethodNoCheckedException(MethodInvocationHelper.java:46)
    at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:146)
    at org.testng.internal.Parameters.handleParameters(Parameters.java:820)
    at org.testng.internal.Parameters.handleParameters(Parameters.java:762)
    at org.testng.internal.ParameterHandler.handleParameters(ParameterHandler.java:60)
    at org.testng.internal.ParameterHandler.createParameters(ParameterHandler.java:39)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:771)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:145)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.util.ArrayList.forEach(Unknown Source)
    at org.testng.TestRunner.privateRun(TestRunner.java:770)
    at org.testng.TestRunner.run(TestRunner.java:591)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:402)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:396)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355)
    at org.testng.SuiteRunner.run(SuiteRunner.java:304)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1180)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1102)
    at org.testng.TestNG.runSuites(TestNG.java:1032)
    at org.testng.TestNG.run(TestNG.java:1000)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
TestRunner类:

package com.qa.test;


import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
import cucumber.api.testng.CucumberFeatureWrapper;
import cucumber.api.testng.TestNGCucumberRunner;

@CucumberOptions(
        features = "/src/main/java/features/login.feature",
        glue = {"stepDefinitions"},
        tags = {"~@Ignore"},
        format = {
                "pretty",
                "html:target/cucumber-reports/cucumber-pretty",
                "json:target/cucumber-reports/CucumberTestReport.json",
                "rerun:target/cucumber-reports/rerun.txt"
        },plugin = "json:target/cucumber-reports/CucumberTestReport.json")
public class testRunner {
    private TestNGCucumberRunner testNGCucumberRunner;


    @BeforeClass
    public void test1() {
        testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
        System.out.println("hi");
    }
    @Test(groups = "cucumber scenarios", description = "Runs Cucumber Scenarios", dataProvider = "myFeatures")
    public void scenario(CucumberFeatureWrapper cucumberFeatureWrapper) throws Throwable {
        testNGCucumberRunner.runCucumber(cucumberFeatureWrapper.getCucumberFeature());
    }

    @DataProvider(name="myFeatures")
    public Object[][] getFeatures()
    {
        return testNGCucumberRunner.provideFeatures();
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>CucumberTest</groupId>
   <artifactId>v1</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>
   <name>v1</name>
   <url>http://maven.apache.org</url>
   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
   <repositories>
      <repository>
         <id>jcenter</id>
         <name>bintray</name>
         <url>http://jcenter.bintray.com</url>
      </repository>
   </repositories>
   <dependencies>
      <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-core -->
      <dependency>
         <groupId>io.cucumber</groupId>
         <artifactId>cucumber-core</artifactId>
         <version>2.4.0</version>
      </dependency>
      <dependency>
         <groupId>io.cucumber</groupId>
         <artifactId>cucumber-java</artifactId>
         <version>2.4.0</version>
      </dependency>
      <dependency>
         <groupId>io.cucumber</groupId>
         <artifactId>cucumber-jvm</artifactId>
         <version>2.4.0</version>
         <type>pom</type>
      </dependency>
      <dependency>
         <groupId>io.cucumber</groupId>
         <artifactId>cucumber-junit</artifactId>
         <version>2.4.0</version>
      </dependency>
      <dependency>
         <groupId>io.cucumber</groupId>
         <artifactId>cucumber-jvm-deps</artifactId>
         <version>1.0.6</version>
      </dependency>
      <dependency>
         <groupId>net.masterthought</groupId>
         <artifactId>cucumber-reporting</artifactId>
         <version>1.0.0</version>
      </dependency>
      <!--      <dependency> -->
      <!--          <groupId>io.cucumber</groupId> -->
      <!--          <artifactId>gherkin</artifactId> -->
      <!--          <version>4.1.3</version> -->
      <!--      </dependency> -->
      <!-- https://mvnrepository.com/artifact/info.cukes/gherkin -->
      <dependency>
         <groupId>info.cukes</groupId>
         <artifactId>gherkin</artifactId>
         <version>2.12.2</version>
         <scope>provided</scope>
      </dependency>
      <dependency>
         <groupId>info.cukes</groupId>
         <artifactId>cucumber-testng</artifactId>
         <version>1.2.5</version>
         <scope>compile</scope>
         <exclusions>
            <exclusion>
               <groupId>junit</groupId>
               <artifactId>junit</artifactId>
            </exclusion>
         </exclusions>
      </dependency>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>3.8.1</version>
      </dependency>
      <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-java</artifactId>
         <version>3.5.3</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-picocontainer -->
      <dependency>
         <groupId>io.cucumber</groupId>
         <artifactId>cucumber-picocontainer</artifactId>
         <version>2.4.0</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-html -->
      <dependency>
         <groupId>io.cucumber</groupId>
         <artifactId>cucumber-html</artifactId>
         <version>0.2.7</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/net.sourceforge.cobertura/cobertura -->
      <dependency>
         <groupId>net.sourceforge.cobertura</groupId>
         <artifactId>cobertura</artifactId>
         <version>1.9.4.1</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core -->
      <dependency>
         <groupId>org.hamcrest</groupId>
         <artifactId>hamcrest-core</artifactId>
         <version>1.3</version>
      </dependency>
      <dependency>
         <groupId>org.testng</groupId>
         <artifactId>testng</artifactId>
         <version>6.10</version>
         <scope>test</scope>
      </dependency>
   </dependencies>
   <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.21.0</version>
            <configuration>
               <suiteXmlFiles>
                  <suiteXmlFile>testNG.xml</suiteXmlFile>
               </suiteXmlFiles>
            </configuration>
         </plugin>
      </plugins>
   </build>
</project>
TestNg.xml:


POM:

package com.qa.test;


import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
import cucumber.api.testng.CucumberFeatureWrapper;
import cucumber.api.testng.TestNGCucumberRunner;

@CucumberOptions(
        features = "/src/main/java/features/login.feature",
        glue = {"stepDefinitions"},
        tags = {"~@Ignore"},
        format = {
                "pretty",
                "html:target/cucumber-reports/cucumber-pretty",
                "json:target/cucumber-reports/CucumberTestReport.json",
                "rerun:target/cucumber-reports/rerun.txt"
        },plugin = "json:target/cucumber-reports/CucumberTestReport.json")
public class testRunner {
    private TestNGCucumberRunner testNGCucumberRunner;


    @BeforeClass
    public void test1() {
        testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
        System.out.println("hi");
    }
    @Test(groups = "cucumber scenarios", description = "Runs Cucumber Scenarios", dataProvider = "myFeatures")
    public void scenario(CucumberFeatureWrapper cucumberFeatureWrapper) throws Throwable {
        testNGCucumberRunner.runCucumber(cucumberFeatureWrapper.getCucumberFeature());
    }

    @DataProvider(name="myFeatures")
    public Object[][] getFeatures()
    {
        return testNGCucumberRunner.provideFeatures();
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>CucumberTest</groupId>
   <artifactId>v1</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>
   <name>v1</name>
   <url>http://maven.apache.org</url>
   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
   <repositories>
      <repository>
         <id>jcenter</id>
         <name>bintray</name>
         <url>http://jcenter.bintray.com</url>
      </repository>
   </repositories>
   <dependencies>
      <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-core -->
      <dependency>
         <groupId>io.cucumber</groupId>
         <artifactId>cucumber-core</artifactId>
         <version>2.4.0</version>
      </dependency>
      <dependency>
         <groupId>io.cucumber</groupId>
         <artifactId>cucumber-java</artifactId>
         <version>2.4.0</version>
      </dependency>
      <dependency>
         <groupId>io.cucumber</groupId>
         <artifactId>cucumber-jvm</artifactId>
         <version>2.4.0</version>
         <type>pom</type>
      </dependency>
      <dependency>
         <groupId>io.cucumber</groupId>
         <artifactId>cucumber-junit</artifactId>
         <version>2.4.0</version>
      </dependency>
      <dependency>
         <groupId>io.cucumber</groupId>
         <artifactId>cucumber-jvm-deps</artifactId>
         <version>1.0.6</version>
      </dependency>
      <dependency>
         <groupId>net.masterthought</groupId>
         <artifactId>cucumber-reporting</artifactId>
         <version>1.0.0</version>
      </dependency>
      <!--      <dependency> -->
      <!--          <groupId>io.cucumber</groupId> -->
      <!--          <artifactId>gherkin</artifactId> -->
      <!--          <version>4.1.3</version> -->
      <!--      </dependency> -->
      <!-- https://mvnrepository.com/artifact/info.cukes/gherkin -->
      <dependency>
         <groupId>info.cukes</groupId>
         <artifactId>gherkin</artifactId>
         <version>2.12.2</version>
         <scope>provided</scope>
      </dependency>
      <dependency>
         <groupId>info.cukes</groupId>
         <artifactId>cucumber-testng</artifactId>
         <version>1.2.5</version>
         <scope>compile</scope>
         <exclusions>
            <exclusion>
               <groupId>junit</groupId>
               <artifactId>junit</artifactId>
            </exclusion>
         </exclusions>
      </dependency>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>3.8.1</version>
      </dependency>
      <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-java</artifactId>
         <version>3.5.3</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-picocontainer -->
      <dependency>
         <groupId>io.cucumber</groupId>
         <artifactId>cucumber-picocontainer</artifactId>
         <version>2.4.0</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-html -->
      <dependency>
         <groupId>io.cucumber</groupId>
         <artifactId>cucumber-html</artifactId>
         <version>0.2.7</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/net.sourceforge.cobertura/cobertura -->
      <dependency>
         <groupId>net.sourceforge.cobertura</groupId>
         <artifactId>cobertura</artifactId>
         <version>1.9.4.1</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core -->
      <dependency>
         <groupId>org.hamcrest</groupId>
         <artifactId>hamcrest-core</artifactId>
         <version>1.3</version>
      </dependency>
      <dependency>
         <groupId>org.testng</groupId>
         <artifactId>testng</artifactId>
         <version>6.10</version>
         <scope>test</scope>
      </dependency>
   </dependencies>
   <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.21.0</version>
            <configuration>
               <suiteXmlFiles>
                  <suiteXmlFile>testNG.xml</suiteXmlFile>
               </suiteXmlFiles>
            </configuration>
         </plugin>
      </plugins>
   </build>
</project>

4.0.0
黄瓜试验
v1
0.0.1-快照
罐子
v1
http://maven.apache.org
UTF-8
jcenter
垃圾箱
http://jcenter.bintray.com
黄瓜
黄瓜核
2.4.0
黄瓜
黄瓜爪哇
2.4.0
黄瓜
黄瓜
2.4.0
聚甲醛
黄瓜
黄瓜刺柏
2.4.0
黄瓜
黄瓜汁
1.0.6
网络智囊团
黄瓜报道
1.0.0
信息杯
小黄瓜
2.12.2
假如
信息杯
黄瓜试验
1.2.5
编译
朱尼特
朱尼特
朱尼特
朱尼特
3.8.1
org.seleniumhq.selenium
硒爪哇
3.5.3
黄瓜
黄瓜皮容器
2.4.0
黄瓜
黄瓜html
0.2.7
net.sourceforge.cobertura
科贝图拉
1.9.4.1
org.hamcrest
汉克雷斯特岩芯
1.3
org.testng
testng
6.10
测试
org.apache.maven.plugins
maven surefire插件
2.21.0
testNG.xml

正如您已经注意到的,实例变量
testNGCucumberRunner
为空。这意味着它没有设置。您正试图在带@BeforeClass注释的方法中设置值,但在调用数据提供程序后会调用此方法

只需在dataprovider而不是@BeforeClass中初始化变量。如果您多次调用数据提供程序,您可以检查变量是否已设置,如果未设置,您可以指定一个值。请参见以下示例:

@DataProvider(name="myFeatures")
public Object[][] getFeatures()
{
    if(testNGCucumberRunner == null){
        testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
    }
    return testNGCucumberRunner.provideFeatures();
}

现在您可以删除方法
test1

,您必须调试
@BeforeClass
方法中发生的事情。