【问题标题】:Spring Boot + Cucumber test: cucumber cannot detect my step definition method due to double quotation escaping in JSONSpring Boot + Cucumber 测试:由于 JSON 中的双引号转义,cucumber 无法检测到我的步骤定义方法
【发布时间】: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


    【解决方案1】:

    我今天在某处读到,从现在开始,正则表达式将被弃用(原因未知),他们正在转向 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 {
        ...
    

    (注意正则表达式不能与黄瓜表达式共存;^$等会导致黄瓜表达式解析错误)

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

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

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

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

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

    【讨论】:

      【解决方案2】:

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

      我认为它对你有用:

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

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

      整个正则表达式是:"binlookup 返回 ",后面是双引号内的所有内容(\"(.*)\" 正则表达式),后面是 " 和状态码 ",后跟一个数字(\d 正则表达式)。

      在stepDef文件中:

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

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

      【讨论】:

      • 谢谢,我注意到我在步骤定义中有一些错误。我更正了它们并使用了黄瓜表达式,现在它可以工作了。
      【解决方案3】:

      使用 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

      【讨论】:

        猜你喜欢
        • 2021-09-06
        • 2012-10-18
        • 1970-01-01
        • 1970-01-01
        • 2017-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多