Java 在jvm中使用多个步骤定义时出现NullPointerException

Java 在jvm中使用多个步骤定义时出现NullPointerException,java,rest,cucumber-jvm,rest-assured,cucumber-serenity,Java,Rest,Cucumber Jvm,Rest Assured,Cucumber Serenity,我目前正在构建一个框架来测试RESTAPI端点。由于我计划编写大量的测试用例,所以我决定组织这个项目,以便重用常用的步骤定义方法 结构如下: FunctionalTest com.example.steps -- AbstractEndpointSteps.java -- SimpleSearchSteps.java com.example.stepdefinitions -- CommonStepDefinition.java

我目前正在构建一个框架来测试RESTAPI端点。由于我计划编写大量的测试用例,所以我决定组织这个项目,以便重用常用的步骤定义方法

结构如下:

FunctionalTest
    com.example.steps
        -- AbstractEndpointSteps.java
        -- SimpleSearchSteps.java
    com.example.stepdefinitions
        -- CommonStepDefinition.java
        -- SimpleSearchStepDefinition.java`
然而,当我尝试调用
SimpleSearchSteps.java
方法时,我得到了一个NullPointerException

commonstep定义代码

package com.example.functionaltest.features.stepdefinitions;

import net.thucydides.core.annotations.Steps;

import com.example.functionaltest.steps.AbstractEndpointSteps;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;


public class CommonStepDefinition {

    @Steps
    private AbstractEndpointSteps endpointSteps;

    @Given("^a base uri \"([^\"]*)\" and base path \"([^\"]*)\"$")
    public void aBaseUriAndBasePath(String baseURI, String basePath) {
        endpointSteps.givenBasepath(baseURI, basePath);
    }

    @When("^country is \"([^\"]*)\"$")
    public void countryIs(String country) 
    {
        endpointSteps.whenCountry(country);
    }

    @Then("^the status code is (\\d+)$")
    public void theStatusCodeIs(int statusCode) {
        endpointSteps.executeRequest();
        endpointSteps.thenTheStatusCodeIs200(statusCode);
    }

}
SimpleSearchStepDefinition.java

package com.example.functionaltest.features.stepdefinitions;

import net.thucydides.core.annotations.Steps;
import com.example.functionaltest.steps.EndpointSteps;
import cucumber.api.java.en.When;


public class SimpleSearchStepDefinition {

    @Steps
    private SimpleSearchSteps searchSteps;



    @When("^what is \"([^\"]*)\"$")
    public void whatIs(String what) {
        searchSteps.whenWhatIsGiven(what);
    }

}
你能看看它是什么,你正试图建立!既然你已经习惯了Cumber,下面是一些空手道(基于Cumber JVM)的东西

  • 内置步骤定义,无需编写Java代码
  • 重新使用*.feature文件并从其他脚本调用它们
  • 动态数据驱动测试
  • 测试的并行执行
  • 每个功能只运行一次某些例程的能力

免责声明:我是开发人员。

看起来您缺少Cucumber注释的holder类,类似这样的内容您应该拥有,以便Cucumber知道并识别您的步骤和功能:

@RunWith(Cucumber.class)
@CucumberOptions(
    glue = {"com.example.functionaltest.features.steps"},
    features = {"classpath:functionaltest/features"}
)
public class FunctionalTest {
}

请注意,在src/test/resources中,您的.feature文件中应该有functionaltest/features文件夹根据此示例,您可以使用ofc,通过您的设计对其进行更改

我通过在AbstractEndpointSteps中使用的静态实例解决了此问题


因此,我能够完全避免步骤定义和NPE问题的重复

本教程规定带注释的
@Steps
字段应为
公共
。感谢@LucianovanderVeekens设置为
公共
,但您的答案仍然存在相同的问题,但添加注释并不能解决NPE问题