Protected by Copyscape
Powered By Blogger

Wednesday, January 15, 2020

What are the ways to validate the response body of Rest APIs using RestAssured?

Generally, we use below 2 ways to validate the response body of any RestAPI using RestAssured.
  • Using Contains method of Collection class.
  • Using JsonPath
If  I take the previous example from my post where I showed the use of HTTP GET method for extraction of weather information. Please use the below example

import io.restassured.RestAssured;
import io.restassured.http.Header;
import io.restassured.http.Headers;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

public class RestGet
{
public static void main(String args[])
{
RestAssured.baseURI="http://restapi.demoqa.com/utilities/weather/city";
RequestSpecification req=RestAssured.given();
Response res=req.get("/Noida");

System.out.println(res.body().asString());
}

}

The output of the above code is as below where we are getting the weather information for Noida.

{
    "City": "Noida",
    "Temperature": "10 Degree celsius",
    "Humidity": "100 Percent",
    "WeatherDescription": "mist",
    "WindSpeed": "2.57 Km per hour",
    "WindDirectionDegree": "343 Degree"
}

Now we have to validate that whether "Noida" as a text available in the response or not. So, if i go with the 1st way then we have to include the follwing line in the above java code, which return a boolean value like True or False.

System.out.println(res.getBody().asString().contains("Noida"));

If I add the above line in the code then the output would be 

{
    "City": "Noida",
    "Temperature": "10 Degree celsius",
    "Humidity": "100 Percent",
    "WeatherDescription": "mist",
    "WindSpeed": "1.96 Km per hour",
    "WindDirectionDegree": "342 Degree"
}
true 


But what happens if you want to extract the individual value in {key,value} pair from response code then we can use the 2nd way i.e. JsonPath.  Also, need to import the below Jar file.

import io.restassured.path.json.JsonPath;

Find the below code for the same.

import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

public class Rest1
{
public static void main(String args[])
{
RestAssured.baseURI="http://restapi.demoqa.com/utilities/weather/city";
RequestSpecification req=RestAssured.given();
Response res=req.get("/Noida");

System.out.println(res.body().asString());
JsonPath j=res.jsonPath();
System.out.println("City value is :"+j.get("City").toString());
System.out.println("Temperature is :"+j.get("Temperature").toString());
if(j.get("City").equals("Noida"))
{
System.out.println("Test case passed");
}
else
{
System.out.println("Test case failed");
}
}

}


Output for the above code:

{
    "City": "Noida",
    "Temperature": "9 Degree celsius",
    "Humidity": "100 Percent",
    "WeatherDescription": "mist",
    "WindSpeed": "1.96 Km per hour",
    "WindDirectionDegree": "342 Degree"
}
City value is :Noida
Temperature is :9 Degree celsius
Test case passed


'**********************************************************************

Please comment if you have any questions on this.

Best Of Luck!

  


No comments: