带斜杠的 Hamcrest 匹配器被解释为验证的一部分 [英] Hamcrest matcher with slashes is interpreted as a part of validation

查看:43
本文介绍了带斜杠的 Hamcrest 匹配器被解释为验证的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下验证,我必须检查返回的正文是否包含包含 "id": 6354 的字符串,但它会解释特殊字符的斜杠.如何验证包含双引号的字符串?

I have the following validation where I have to check if returned body has a string containing "id": 6354, but it interprets slashes of special characters. How I can validate strings which contain double quotation marks ?

代码

import static org.hamcrest.Matchers.containsString;
import com.jayway.restassured.response.Response;


    response.then()
            .body(containsString("\"id\": 6354"));

错误

Response body doesn't match expectation.
Expected: a string containing "\"id\": 6354"
  Actual: {...,"id": 6354, ...}

推荐答案

Hamcrest containsString 似乎在输出错误消息中打印转义字符,但在进行匹配时似乎正确转义它们.

Hamcrest containsString seems to print the escaped characters in the output error message, however it seems to correctly escape them when doing the matching.

在我的示例中,我错误地添加了一个空格,因此请按照问题中的示例进行操作:"id": 6354 会给出错误 Expected: a string contains "\"id\": 6354" 但是当我将其更改为 "id" 时:6354",断言通过.

In my example, I was incorrectly adding a space, so following the example in the question: "id": 6354 would give the error Expected: a string containing "\"id\": 6354" however when I changed it to "id":6354", it passed the assertion.

这篇关于带斜杠的 Hamcrest 匹配器被解释为验证的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆