I want to compare two json response. I have tried to store the JSON response in a pojo then compared by overiding equals method but my pojo is very large and consist of 10 classes . Is there any other way to compare
You may try below code snippet:
import org.codehaus.jettison.json.JSONObject;
import org.junit.Assert;
import io.restassured.RestAssured;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.filter.cookie.CookieFilter;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
Retrive JSON responses:
Response jsonResponseOne = RestAssured.given().spec(requestSpecification).filter(cookieFilter).relaxedHTTPSValidation().when().///.then().assertThat().extract().response();
Response jsonResponseTwo = RestAssured.given().spec(requestSpecification).filter(cookieFilter).relaxedHTTPSValidation().when().///.then().assertThat().extract().response();
Extract both JSON responses as a string:
JSONObject jsonOne = new JSONObject(jsonResponseOne.getBody().asString())
JSONobject jsonTwo = new JSONObject(jsonResponseTwp.getBody().asString())
Compare response using Java:
Assert.assertEquals(jsonOne, jsonTwo);
If you need the differences between them, try to use zjsonpatch library to detect the differences between two json responses.
A sample code:
JsonNode json1= jacksonObjectMapper.readTree(jsonString1); JsonNode json2= jacksonObjectMapper.readTree(jsonString2); JsonNode patchNode = JsonDiff.asJson(json1, json2); String diffs = patchNode.toString();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.