我正在尝试在 Serenity 框架中使用 Rest Assured 来验证端点响应。我将一个 xml 正文发送到端点,并期望返回一个 JSON 响应,如下所示:

{"Entry ID" : "654123"}

我想发送 XML 并在 JSON 响应中验证键“条目 ID”的值不为空或为空。问题是, key 中有一个空格,我相信它会导致错误。这是我到目前为止所拥有的:
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 中获取包含空格的键的值?

最佳答案

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

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()));

关于java - 如果 key 在 Rest Assured/Serenity 中包含空格,如何获取 key 的值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45112425/

10-12 06:09