jbo*_*boz 6 testing bdd cucumber spring-boot
我尝试在Cucumber测试中使用MockMvc,但没有解决Spring依赖关系.
我创建了这个类:
@RunWith(Cucumber.class)
@CucumberOptions(format = "pretty", features = "src/test/resources/features"})
@SpringBootTest
public class CucumberTest {
}
Run Code Online (Sandbox Code Playgroud)
运行黄瓜功能
这个课程的步骤:
@WebMvcTest(VersionController.class)
@AutoConfigureWebMvc
public class VersionControllerSteps {
@Autowired
private MockMvc mvc;
private MvcResult result;
@When("^the client calls /version$")
public void the_client_issues_GET_version() throws Throwable {
result = mvc.perform(get("/version")).andDo(print()).andReturn();
}
@Then("^the client receives status code of (\\d+)$")
public void the_client_receives_status_code_of(int statusCode) throws Throwable {
assertThat(result.getResponse().getStatus()).isEqualTo(statusCode);
}
@And("^the client receives server version (.+)$")
public void the_client_receives_server_version_body(String version) throws Throwable {
assertThat(result.getResponse().getContentAsString()).isEqualTo(version);
}
}
Run Code Online (Sandbox Code Playgroud)
但这抛出异常:
java.lang.NullPointerException
at com.example.rest.VersionControllerSteps.the_client_issues_GET_version(VersionControllerSteps.java:30)
at ?.When the client calls /version(version.feature:8)
Run Code Online (Sandbox Code Playgroud)
这是.feature:
Feature: the version can be retrieved
As a api user
I want to know which api version is exposed
In order to be a good api user
Scenario: client makes call to GET /version
When the client calls /version
Then the client receives status code of 200
And the client receives server version 1.0
Run Code Online (Sandbox Code Playgroud)
如何配置我的测试使用黄瓜和弹簧启动?
提前致谢.
从您的代码清单和错误日志来看,尚不清楚这是 cucumber+spring 设置的问题还是只是应用程序错误。
堆栈跟踪指向第 30 行,该行抛出空指针异常。从您的代码清单来看,这可能是由于result.getResponse().getContentAsString()说明造成的。
可能值得检查您的控制器是否确实返回了主体。例如,您可能需要用注释标记返回@ResponseBody值
@RequestMapping(
value = "/campaigns/new",
method = RequestMethod.GET,
)
public @ResponseBody String vers() {
return "1.0.1";
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1275 次 |
| 最近记录: |