我目前正在构建一个框架来测试 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/