REST Assured 系列汇总 之 REST Assured 72 - How To Compare Part Of JSON Objects And Arrays Using JSONassert Library
介绍
很多时候我们只需比较特定某部分的 JSON Objects 或 JSON Arrays。例如:假设我们创建一个新的订单并获取订单细节。当我们获取所有订单信息时,我们只需要验证刚创建的那个新的订单,不是验证所有的订单。
前提条件
添加 JSONassert 依赖
<!-- https://mvnrepository.com/artifact/org.skyscreamer/jsonassert -->
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.5.0</version>
<scope>test</scope>
</dependency>
请先阅读下面文章,了解 JSONassert Java library 基本概念 ,不同的比较模式和断言 JSON Objects 和 Array。
Introduction To JsonAssert Library
Compare JSON Objects Using JSONassert Library
Compare JSON Arrays Using JSONassert Library
比较部分 JSON Objects
JSON Object 1
{
"lastName": "Animesh",
"firstName": "Prashant",
"address": {
"city": "Kolkata",
"state": "WB"
}
}
JSON Object 2
{
"lastName": "Animesh",
"firstName": "Prashant",
"communicationAddress": {
"city": "Kolkata",
"state": "WB"
},
"skills": [
"CA",
"BCOM"
]
}
我们需要比较第一个 JSON Object 中的 “address” object 和第二个 JSON Object 中的 “communicationAddress” object 的值是否相等。我们分别用 JsonPath 的方法来获取想要的部分 JSON Objects,然后用 assertEquals() 方法来比较。
import org.json.JSONException;
import org.json.JSONObject;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
public class ComparePortionOfJsonObjects {
public static void main(String[] args) throws JSONException {
JSONObject jsonObject1 = new JSONObject("{\r\n" +
" \"lastName\": \"Animesh\",\r\n" +
" \"firstName\": \"Prashant\",\r\n" +
" \"address\": {\r\n" +
" \"city\": \"Kolkata\",\r\n" +
" \"state\": \"WB\"\r\n" +
" }\r\n" +
"}");
JSONObject jsonObject2 = new JSONObject("{\r\n" +
" \"lastName\": \"Animesh\",\r\n" +
" \"firstName\": \"Prashant\",\r\n" +
" \"communicationAddress\": {\r\n" +
" \"city\": \"Kolkata\",\r\n" +
" \"state\": \"WB\"\r\n" +
" },\r\n" +
" \"skills\": [\r\n" +
" \"CA\",\r\n" +
" \"BCOM\"\r\n" +
" ]\r\n" +
"}");
// I want to assert address objects are same in both
// Since "address" and "communicationAddress" are JSON Objects so we need to use proper method i.e. getJSONObject()
JSONAssert.assertEquals(jsonObject1.getJSONObject("address"), jsonObject2.getJSONObject("communicationAddress"), JSONCompareMode.LENIENT);
}
}
比较部分 JSON Arrays
JSON Array 1
[
{
"id": 1,
"first_name": "Giavani",
"last_name": "Skellorne"
},
{
"id": 2,
"first_name": "Patton",
"last_name": "Buchett"
}
]
JSON Array 2
[
{
"id": 1,
"first_name": "Giavani",
"last_name": "Skellorne"
},
{
"id": 2,
"first_name": "Harland",
"last_name": "Brookwood"
},
{
"id": 3,
"first_name": "Leigh",
"last_name": "Gatteridge"
}
]
上两个 JSON Array,想比较比较数组中的第一个 objects
import org.json.JSONArray;
import org.json.JSONException;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
public class ComparePortionOfJsonArrays {
public static void main(String[] args) throws JSONException {
JSONArray jSONArray1 = new JSONArray("[\r\n" +
" {\r\n" +
" \"id\": 1,\r\n" +
" \"first_name\": \"Giavani\",\r\n" +
" \"last_name\": \"Skellorne\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"id\": 2,\r\n" +
" \"first_name\": \"Patton\",\r\n" +
" \"last_name\": \"Buchett\"\r\n" +
" }\r\n" +
"]");
JSONArray jSONArray2 = new JSONArray("[\r\n" +
" {\r\n" +
" \"id\": 1,\r\n" +
" \"first_name\": \"Giavani\",\r\n" +
" \"last_name\": \"Skellorne\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"id\": 2,\r\n" +
" \"first_name\": \"Harland\",\r\n" +
" \"last_name\": \"Brookwood\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"id\": 3,\r\n" +
" \"first_name\": \"Leigh\",\r\n" +
" \"last_name\": \"Gatteridge\"\r\n" +
" }\r\n" +
"]");
// I want to assert address objects are same in both
// Since index elements in both arrays are JSON objects so using proper method i.e. getJSONObject
JSONAssert.assertEquals(jSONArray1.getJSONObject(0), jSONArray2.getJSONObject(0), JSONCompareMode.LENIENT);
}
}
你也可以比较 JSON Object 中的一部分和 JSON Array 中的一部分。但是要注意,比较的部分一定要是相同的 Type,不然编译会出错。