【发布时间】:2017-12-20 02:42:55
【问题描述】:
我正在尝试在 Serenity 框架中使用 Rest Assured 来验证端点响应。我向端点发送了一个 xml 正文,并期望得到一个 JSON 响应,如下所示:
{"Entry ID" : "654123"}
我想发送 XML 并在 JSON 响应中验证键“条目 ID”的值不为空或 null。问题是,密钥中有一个空格,我相信它会导致错误。这是我目前所拥有的:
SerenityRest.given().contentType(ContentType.XML)
.body(xmlBody)
.when().accept(ContentType.JSON).post(endpoint)
.then().body("Entry ID", not(isEmptyOrNullString()))
.and().statusCode(200);
这会产生错误:
java.lang.IllegalArgumentException: Invalid JSON expression:
Script1.groovy: 1: unable to resolve class Entry
@ line 1, column 33.
Entry ID
^
1 error
我尝试以不同的方式包装“条目 ID”术语,但无济于事:
.body("'Entry ID'", not(isEmptyOrNullString()))
.body("''Entry ID''", not(isEmptyOrNullString()))
.body("\"Entry ID\"", not(isEmptyOrNullString()))
.body("['Entry ID']", not(isEmptyOrNullString()))
.body("$.['Entry ID']", not(isEmptyOrNullString()))
是否可以在 Rest Assured 中获取包含空格的键的值?
【问题讨论】:
-
切换到空手道 [github.com/intuit/karate] 没有这个问题,这里有一个例子:github.com/intuit/karate/blob/…
-
我的想法是肯定有其他问题。 body("['Entry ID']") 是正确的方法。 stackoverflow.com/questions/34074569/… 它适用于放心使用的 jayway json 路径。也许尝试使用路径抓取它,然后验证它? github.com/rest-assured/rest-assured/wiki/Usage#single-path
标签: java json rest rest-assured serenity-bdd