RestAssured:从具有指定值的 json 对象获取键值
RestAssured: get key value from json object with specified value
在一个测试用例中,我执行了一个具有以下响应的调用:
[
{
"ourID": "770050010000000010",
"date": "2019-03-07",
"otherValue": null
},
{
"ourID": "770050010000000020",
"date": "2019-03-07",
"otherValue": null
}
]
测试由Serenity执行,我使用RestAssured执行调用:
Response response = RestAssuredApiHelper.baseRestCall(headers, baseUrl, path)
.body(requestBody)
.post();
assertThat(response.getBody().jsonPath().get("$.[?(@.ourID=='770050010000000010')].date"), contains("2019-03-07"));
断言不起作用,可能是因为 RestAssured 使用 Gpath 而不是 JsonPath,但在我阅读的所有文档中,都有这种方法的示例。
我在这里错过了什么?
我得到的错误是:
java.lang.IllegalArgumentException: Invalid JSON expression:
Script1.groovy: 1: unexpected token: [ @ line 1, column 29.
$.[?(@.meteringPointEANID=='770050010000000010')].energySource
^
1 error
用下面的代码检查:--(我相信 json 表达式是正确的,没有检查)
Object dateObject = JsonPath.parse(response.getBody().asString()).read("$.[?(@.ourID=='770050010000000010')].date");
String dateString = dataObject.toString();
assertThat(dateString, containsString("2019-03-07"));
POM 依赖项:---
<!-- https://mvnrepository.com/artifact/com.jayway.jsonpath/json-path -->
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.1.0</version>
</dependency>
将 JsonPath 与 RestAssured 一起使用的替代方法是使用 RestAssured 的内置反序列化机制。
如果您不知道对象的 return 类型是什么,您可以查看以下内容:
https://github.com/rest-assured/rest-assured/wiki/usage#deserialization-with-generics
通用解决方案如下所示:
Response response = RestAssuredApiHelper.baseRestCall(headers, baseUrl, path)
.body(requestBody)
.post();
List<Map<String, Object>> objects = response.getBody().as(new TypeRef<>() {});
Map<String, Object> singleObject = objects.stream().filter((Map<String, Object> object) -> object.get("ourID").equals("770050010000000010"))
.peek(System.out::println)
.findFirst().get();
System.out.println(singleObject.get("ourID")); // 770050010000000010
知道响应对象是什么就更简单了:
List<YourResponseObject> yourResponseObjects = response.getBody().as(new TypeRef<>() {});
assertEquals(response.path("find {it.ourID == '770050010000000010'}.date"), "2019-03-07")
在一个测试用例中,我执行了一个具有以下响应的调用:
[
{
"ourID": "770050010000000010",
"date": "2019-03-07",
"otherValue": null
},
{
"ourID": "770050010000000020",
"date": "2019-03-07",
"otherValue": null
}
]
测试由Serenity执行,我使用RestAssured执行调用:
Response response = RestAssuredApiHelper.baseRestCall(headers, baseUrl, path)
.body(requestBody)
.post();
assertThat(response.getBody().jsonPath().get("$.[?(@.ourID=='770050010000000010')].date"), contains("2019-03-07"));
断言不起作用,可能是因为 RestAssured 使用 Gpath 而不是 JsonPath,但在我阅读的所有文档中,都有这种方法的示例。 我在这里错过了什么?
我得到的错误是:
java.lang.IllegalArgumentException: Invalid JSON expression:
Script1.groovy: 1: unexpected token: [ @ line 1, column 29.
$.[?(@.meteringPointEANID=='770050010000000010')].energySource
^
1 error
用下面的代码检查:--(我相信 json 表达式是正确的,没有检查)
Object dateObject = JsonPath.parse(response.getBody().asString()).read("$.[?(@.ourID=='770050010000000010')].date");
String dateString = dataObject.toString();
assertThat(dateString, containsString("2019-03-07"));
POM 依赖项:---
<!-- https://mvnrepository.com/artifact/com.jayway.jsonpath/json-path -->
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.1.0</version>
</dependency>
将 JsonPath 与 RestAssured 一起使用的替代方法是使用 RestAssured 的内置反序列化机制。
如果您不知道对象的 return 类型是什么,您可以查看以下内容: https://github.com/rest-assured/rest-assured/wiki/usage#deserialization-with-generics
通用解决方案如下所示:
Response response = RestAssuredApiHelper.baseRestCall(headers, baseUrl, path)
.body(requestBody)
.post();
List<Map<String, Object>> objects = response.getBody().as(new TypeRef<>() {});
Map<String, Object> singleObject = objects.stream().filter((Map<String, Object> object) -> object.get("ourID").equals("770050010000000010"))
.peek(System.out::println)
.findFirst().get();
System.out.println(singleObject.get("ourID")); // 770050010000000010
知道响应对象是什么就更简单了:
List<YourResponseObject> yourResponseObjects = response.getBody().as(new TypeRef<>() {});
assertEquals(response.path("find {it.ourID == '770050010000000010'}.date"), "2019-03-07")