java - 在 Cucumber-jvm 中使用多个步骤定义时出现 NullPointerException

标签 java rest cucumber-jvm rest-assured cucumber-serenity

我目前正在构建一个框架来测试 Rest-API 端点。由于我计划编写大量测试用例,因此我决定组织该项目以允许我重用常见的步骤定义方法。

结构如下;

FunctionalTest
    com.example.steps
        -- AbstractEndpointSteps.java
        -- SimpleSearchSteps.java
    com.example.stepdefinitions
        -- CommonStepDefinition.java
        -- SimpleSearchStepDefinition.java`

但是,当我尝试调用 SimpleSearchSteps.java 方法时,我收到 NullPointerException

CommonStepDefinition代码

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);
    }

}

最佳答案

看起来您缺少 Cucumber 注释的持有者类,您应该拥有这样的东西,以便 Cucumber 知道并识别您的步骤和功能:

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

请注意,根据此示例,在您的 src/test/resources 中,您应该具有包含 .feature 文件的功能测试/features 文件夹,您可以根据您的设计更改它

关于java - 在 Cucumber-jvm 中使用多个步骤定义时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44908994/

相关文章:

java HttpURLConnection.setRequestMethod() 不起作用

java - 如何从变量设置 jspinner 最大值?

c# - 如何在 C# 中实现与在 Java 中相同的覆盖体验?

java - 从数字中解析并检查日期的有效性

mysql - 无法根据 jhipster 中的日期字段=当前日期获取记录

java - 当第一个场景失败时如何跳过所有 cucumber-jvm 场景

angularjs - Cucumber-java 与 Cucumber.js

java - Dropwizard Rest API 的 JSON 正文开头处存在无效字符

web-services - Moodle 2.9.1 -- 任何网络服务功能都会给出 'access control exception'

java - Allure Cucumber JVM 适配器 - 报告显示没有功能没有故事