How to validate JSON body in Rest Assured?

HOME

 <dependency>
      <groupId>io.rest-assured</groupId>
      <artifactId>json-schema-validator</artifactId>
      <version>5.3.2</version>
 </dependency>

{
  "data": {
    "id": 3,
    "email": "emma.wong@reqres.in",
    "first_name": "Emma",
    "last_name": "Wong",
    "avatar": "https://reqres.in/img/faces/3-image.jpg"
  },
  "support": {
    "url": "https://reqres.in/#support-heading",
    "text": "To keep ReqRes free, contributions towards server costs are appreciated!"
  }
}

import static io.restassured.module.jsv.JsonSchemaValidator.

import org.junit.Test;
import java.io.IOException;
import static io.restassured.RestAssured.given;
import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;

public class JsonCompare {

    @Test
    public void verifyGreaterResponseTime() throws IOException {

        // Given
        given()

                // When
                .when()
                .get("https://reqres.in/api/users/3")

                // Then
                .then()

                .assertThat()
                .body(matchesJsonSchemaInClasspath("User.json"));
    }
}

Leave a comment