我目前正在构建一个测试Rest-API端点的框架。当我计划编写大量测试用例时,我决定组织该项目以允许我重用常用的步骤定义方法。
结构如下;
FunctionalTest
com.example.steps
-- AbstractEndpointSteps.java
-- SimpleSearchSteps.java
com.example.stepdefinitions
-- CommonStepDefinition.java
-- SimpleSearchStepDefinition.java`
但是当我尝试调用SimpleSearchSteps.java
方法时,我得到一个NullPointerException
CommonStepDefinition Code
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);
}
}
答案 0 :(得分:1)
看起来你缺少Cucumber注释的持有者类,你应该这样,黄瓜知道并确定你的步骤和功能:
@RunWith(Cucumber.class)
@CucumberOptions(
glue = {"com.example.functionaltest.features.steps"},
features = {"classpath:functionaltest/features"}
)
public class FunctionalTest {
}
请注意,在您的src / test / resources中,您应该根据此示例包含带有.feature文件的functionaltest / features文件夹,您可以根据您的设计进行更改
答案 1 :(得分:0)
你能看一下Karate这正是你想要建立的!既然你已经习惯了Cucumber,这里有一些空手道provides as enhancements (基于Cucumber-JVM)
免责声明:我是开发者。
答案 2 :(得分:0)
我在AbstractEndpointSteps中使用RequestSpecBuilder的静态实例而不是RequestSpecification来解决此问题。
因此,我能够完全避免重复StepDefinitions和NPE问题