小编jbo*_*boz的帖子

@RunWith(Cucumber.class)和@Autowired MockMvc

我尝试在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) …
Run Code Online (Sandbox Code Playgroud)

testing bdd cucumber spring-boot

6
推荐指数
1
解决办法
1275
查看次数

标签 统计

bdd ×1

cucumber ×1

spring-boot ×1

testing ×1