【发布时间】:2018-08-28 15:33:48
【问题描述】:
在 Spring Boot REST 应用程序中,我想使用 Cucumber-jvm 检查返回的 JSON 是否正是我所期望的。但是,由于我必须在 JSON 键名周围使用双引号,Cucumber 无法检测到正确的步骤定义方法,因此测试无法通过。
这是预期的 JSON 结果:
{"fields":[],"errorMsg":"BIN not found"}
黄瓜步骤定义:
Given bin number is <bin>
When binlookup searches with this bin number
Then binlookup returns <result> and status code <code>
Examples:
| bin | result | code |
| "222222" | "{\"fields\":[\"bin\"]\,\"errorMsg\":\"Invalid Argument\"}" | 404 |
对应的方法:
@Then("^binlookup returns \"([^\"]*)\" and status code \\d$")
public void binlookup_returns_and_status_code(String result, Integer code) throws Exception {
assertThat(this.results.getResponse().getContentType()).isEqualTo(MediaType.APPLICATION_JSON_UTF8_VALUE);
assertThat(this.results.getResponse().getStatus()).isEqualTo(code);
assertThat(this.results.getResponse().getContentAsString().length()).isNotEqualTo(0);
assertThat(this.results.getResponse().getContentAsString()).isEqualTo(result);
}
运行测试时,我确实返回了正确的 JSON:
{"fields":["bin"],"errorMsg":"Invalid Argument"}
但我看到测试错误,Cucumber 无法检测到我的方法,并给了我如下提示:
You can implement missing steps with the snippets below:
@Then("binlookup returns {string}\\:[],\\{string}\\:\\{string} and status code {int}")
public void binlookup_returns_and_status_code(String string, String string2, String string3, Integer int1) {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
显然,它将第一个" 与第一个转义的" 配对,并将{\"fields 视为第一个参数,但这是错误的。
但是,我不能用 ' ' 引用 JSON 字符串,因为情况并非如此。
我能做什么?
如果不可能,如何验证 JSON 是否包含我期望的数据?
【问题讨论】:
标签: java json cucumber spring-test