SachinB SachinB - 1 year ago 173
JSON Question

Rest Assured Framework complete JSON response matching

I am using Rest Assured Framework for API testing(Using Java).
At line (1),I am expecting error as there is mismatch in expected JSON response and Actual JSON response
But instead my code is executing successfully.
Can someone please tell me if I am doing anything wrong in below code?

public void test123() {
try {
//Read the Curl Request Input file
String json = input.readFromTextFile(
System.getProperty("user.dir") + "\\src\\test\\resources\\inputFile\\CurlDataFile.txt");
json = json.replaceAll(" ", "");
RestAssured.baseURI = "My URL";
assertThat().body(matchesJsonSchemaInClasspath("testCurlOuput1.json")); (1)
} catch (IOException e) {
}catch(JsonSchemaValidationException e){

Answer Source

Finally I choose different library i.e. jayway.restassured library and then JSON Assert library (org.skyscreamer.jsonassert.JSONAssert) which will comapre actual and expected response.

public void test123() {         
String postData = input.readFromTextFile(System.getProperty("user.dir") + "\\src\\test\\resources\\inputFile\\CurlDataFile.txt");
        RestAssured.baseURI = "MY URL";
Response r = (Response)given().contentType("application/json").body(postData).when().post("");
            String responseBody = r.getBody().asString();           
            String curlResponse = //I am providing expected Curl response here          
       //JSON Assertion for matching Expected and Actual response
            JSONAssert.assertEquals(curlResponse, responseBody, false);

Also sometime we may want to avoid comparing particular field from JSON like some ID field which generate dynamically which we can do using JSON comparator

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download