Peter
Peter

Reputation: 79

How do you compare an entire Json file to it's response using restassured?

I'm using restassured something I used a few years back. When doing so I had a folder with Json files in my project. I was comparing these to the actual outcome of the API response. What is the best way to go about this. I obviously need my file location in my project and need to compare it to my response. Is there a standard way to do this.

So originally I had this. Just to check the City from the body but I want the whole thing.

    @Test
public void GetCity() {

    given().
            when().
            get(city).
            then().
            assertThat().
            body(("city"), equalTo(city));
}

But I want to get to something like this below:

@Test
public void GetCity() {

    given().
            when().
            get(city).
            then().
            assertThat().
            JsonFile(("/Myjson"), equalTo(response));
}

I'm using TestNg currently but I remembered using Cucumber Scenarios which allowed me to test multiple responses in a data table. Question I have is how do I achieve the above?

    {
  "id": 25,
  "first_name": "Caryl",
  "last_name": "Ruberry",
  "email": "[email protected]",
  "ip_address": "222.10.201.47",
  "latitude": 11.8554828,
  "longitude": -86.2183907,
  "city": "Dolores"
}

Upvotes: 1

Views: 3410

Answers (1)

Fenio
Fenio

Reputation: 3635

What I understood from the question is to get response from an API and compare with JSON file. How do it:

 @Test
public void GetCity() {
        Response response = when().
            get(city).
        then().
            extract()
            response();

}

First, we extract the Response object which contains information like status code or response body. In this case it will be JSON. Before we extract it, let's create a POJO with JSON representation:

{
  "id": 25,
  "first_name": "Caryl",
  "last_name": "Ruberry",
  "email": "[email protected]",
  "ip_address": "222.10.201.47",
  "latitude": 11.8554828,
  "longitude": -86.2183907,
  "city": "Dolores"
}

The above JSON can be represented by below class:

public class UserEntity {
    public Long id; //id is exact name field in JSON
    @JsonProperty("first_name"); //other approach
    public String firstName;
    public String last_name;
    public String email;
    public String ip_address;
    public Long latitude;
    public Long longitude;
    public String city;
} 

Now, we can transform the JSON response body into this class like this:

 @Test
public void GetCity() {
        Response response = when().
            get(city).
        then().
            extract()
            response();
        UserEntity userEntityResponse = response.jsonPath().getObject("$", UserEntity.class);
}

The "$" means root of JSON file (the first object {}). This is how Response is translated into POJO. We can do it in a very similar matter

        Response response = when().
            get(city).
        then().
            extract()
            response();
        UserEntity userEntityResponse = response.jsonPath().getObject("$", UserEntity.class);
        UserEntity userEntityFile = JsonPath.from(new File("file path"));

Now you can easily compare them like:

assertEquals(userEntityFile.id, userEntityResponse.id);

You could also override hashCode() and equals() methods but that might be too much if you're just learning :)

Upvotes: 1

Related Questions