Spring Boot + Cucumber 测试:由于双引号转义,cucumber 无法检测到我的步骤定义方法 JSON

Spring Boot + Cucumber test: cucumber cannot detect my step definition method due to double quotation escaping in JSON

在 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 是否包含我期望的数据?

黄瓜的 stepdefs 都是关于正则表达式的。参数是使用捕获组捕获的,您只需要使用匹配 json.

的正则表达式

我认为这对你有用:

@Then("^binlookup returns \"(.*)\" and status code \d$")

\"(.*)\" 正则表达式将捕获双引号内的所有内容。

整个正则表达式为:"binlookup returns ",后跟双引号内的所有内容(\"(.*)\" 正则表达式),后跟 ”和状态代码“,后跟一个数字(\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 {
    ...

(注意regex 不能和cucumber表达式共存;^$等会导致cucumber表达式解析错误)

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

...
Then binlookup returns '{"fields":[],"errorMsg":"BIN not found"}' and status code 404 # BinInfoControllerCucumberTests.binlookup_returns_and_status_code(String,Integer)

可以看到方法找到了。之前是null(找不到).

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

记住:在字符串中,只需要转义你用来包围整个字符串的符号,可以是单引号或双引号。

使用 Cucumber-JVM 5 (https://github.com/cucumber/cucumber-jvm/blob/main/release-notes/v5.0.0.md) 中引入的 @DocStringType Cucumber 表达式,而不是使用正则表达式

你可以在这里找到一个例子https://blog.executeautomation.com/all-new-cucumber-jvm-5-with-its-cucumber-expression