Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 弹簧靴&x2B;Cucumber测试:Cucumber无法检测我的步骤定义方法,因为JSON中有双引号转义_Java_Json_Cucumber_Spring Test - Fatal编程技术网

Java 弹簧靴&x2B;Cucumber测试:Cucumber无法检测我的步骤定义方法,因为JSON中有双引号转义

Java 弹簧靴&x2B;Cucumber测试:Cucumber无法检测我的步骤定义方法,因为JSON中有双引号转义,java,json,cucumber,spring-test,Java,Json,Cucumber,Spring Test,在SpringBootREST应用程序中,我想通过jvm检查返回的JSON是否正是我所期望的。但是,由于我必须在JSON键名称周围使用双引号,Cucumber无法检测到正确的步骤定义方法,因此测试无法通过 以下是预期的JSON结果: {"fields":[],"errorMsg":"BIN not found"} 步骤定义: Given bin number is <bin> When binlookup searches with this bin number Then bin

在SpringBootREST应用程序中,我想通过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();
}
显然,它将第一个
与第一个转义的
配对,并将
{\”字段作为第一个参数,但这是错误的

但是,我不能用
'
引用JSON字符串,因为情况并非如此

我能做什么



如果不可能,我如何验证JSON是否具有我期望的数据?

cucumber的stepdefs是关于regex的。参数是使用捕获组捕获的,您只需要使用与JSON匹配的regex

我想这会对你有用:

@Then("^binlookup returns \"(.*)\" and status code \\d$")
“(.*)”正则表达式将捕获双引号内的所有内容

整个正则表达式是:“binlookup returns”,后跟双引号内的所有内容(\“(.*)\”正则表达式),后跟“and status code”,后跟一个数字(d正则表达式)

在stepDef文件中:

Examples: 
| bin      | result code |
| "222222" | "{"fields":["bin"],"errorMsg":"Invalid Argument"}"   | 404  |

请注意,您不需要使用这种方法来转义json中的双引号。

我今天在某个地方读到,从现在开始,正则表达式将被弃用(原因未知),它们将转向Cucumber表达式。我使用Cucumber 3.0.2,其中Cucumber表达式可用。所以我尝试了一下,突然,一切都好了

我还注意到我在OP中有一些错误,我也纠正了它们

我还发现,您可以在整个字符串周围使用单引号,因此,如果有许多双引号需要转义,则应在整个字符串周围使用单引号,这样可以避免转义双引号

现在我有:

Examples: 
  | bin      | result                                                       | code |
  | "222222" | '{"fields":[],"errorMsg":"BIN not found"}'                   | 404  |
方法注释如下:

@Then("binlookup returns {string} and status code {int}")
public void binlookup_returns_and_status_code(String result, Integer code) throws Exception {
    ...
(请注意,正则表达式不能与cucumber表达式共存;
^
$
以及其他内容将导致cucumber表达式中的解析错误)

我可以通过所有的测试。至少在Eclipse中。在IntelliJ中我不知道

...
Then binlookup returns '{"fields":[],"errorMsg":"BIN not found"}' and status code 404 # BinInfoControllerCucumberTests.binlookup_returns_and_status_code(String,Integer)
您可以看到找到了该方法。之前的was
null
(找不到)

单引号+正则表达式不起作用


记住:在字符串中,只需转义用于环绕整个字符串的符号,可以是单引号或双引号。

谢谢,我注意到我在步骤定义中有一些错误。我更正了它们并使用了cucumber表达式,现在它可以工作了。