我在testNGCucumberRunner.provideFeatures();中收到NullPointerException

我尝试调试,它显示testNGCucumberRunner具有空值。我正在尝试将Cucumber与TestNG一起使用。
在这里发布我的代码和异常。我尝试将测试运行器类放入不同的程序包中,并将代码放入测试运行器以外的其他类中。也尝试过使用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();
    }
}


TestNg.xml:


<suite name="Suite1" verbose="1" >
  <test name="Login" >
    <classes>
<class name="com.qa.test.testRunner" />
    </classes>
  </test>

</suite>


POM:

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

最佳答案

您已经注意到实例变量testNGCucumberRunner为null。这意味着未设置。您正在尝试使用@BeforeClass注释方法在您的值中设置值,但是在调用dataprovider后将调用此方法。

只需在dataprovider中而不是@BeforeClass中初始化变量即可。我是如果您多次调用dataprovider,则可以检查变量是否已设置,如果没有设置,则分配一个值。请参见以下示例:

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


现在,您可以删除方法test1

关于java - testNGCucumberRunner.provideFeatures中的空指针异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58040583/

10-12 20:48