【问题标题】:How do I get the value of a key if it contains a space in Rest Assured / Serenity?如果键在 Rest Assured / Serenity 中包含空格,我如何获取键的值?
【发布时间】: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 中获取包含空格的键的值?

【问题讨论】:

标签: java json rest rest-assured serenity-bdd


【解决方案1】:

你只需要用单引号转义键:

then().body("'Entry ID'", not(isEmptyOrNullString()))

这是一个示例(在 3.0.6 版本中测试):

// Given
String json = "{\"Entry ID\" : \"654123\"}";

// When
JsonPath jsonPath = JsonPath.from(json);

// Then
assertThat(jsonPath.getString("'Entry ID'"), not(isEmptyOrNullString()));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 2020-07-29
    • 1970-01-01
    • 1970-01-01
    • 2022-10-16
    • 2019-07-15
    • 2017-02-28
    相关资源
    最近更新 更多