Home » restAssured-tutorial » Test Rest Api using Rest Assured

Newly Updated Posts

Test Rest Api using Rest Assured

In previous tutorial we have how to Configure Eclipse with Rest-Assured API, In this tutorial we will be learning how to write and Test Rest Api using Rest Assured.

Test Rest Api using Rest Assured

To understand in depth, we will are taking a simple Rest Api and the details of this Api is provided below.

End Point->https://rahulshettyacademy.com

Resource->/maps/api/place/add/json

Query Parameter -> key =qaclick123

Http method-> POST

Sample Request Json->

{
“location”: {
“lat”: -38.383494,
“lng”: 33.427362
},
“accuracy”: 50,
“name”: “Frontline house”,
“phone_number”: “(+91) 983 893 3937”,
“address”: “29, side layout, cohen 09”,
“types”: [
“shoe park”,
“shop”
],
“website”: “http://google.com”,
“language”: “French-IN”
}

Sample Response Json->

{
“status”: “OK”,
“place_id”: “928b51f64aed18713b0d164d9be8d67f”,
“scope”: “APP”,
“reference”: “736f3c9bec384af62a184a1936d42bb0736f3c9bec384af62a184a1936d42bb0”,
“id”: “736f3c9bec384af62a184a1936d42bb0”
}

In order to get the above response, we will follow the below steps through java code using rest assured api

1) We will use Rest assured class to create a request body and provide the required parameters to the request body

2) Will define the https type

3) Sending request to the server

4) Getting back the response from the server

5) Display the response in console of eclipse

How to Write and Test Rest Api using Rest Assured

Find Below code to hit the request from Rest Assured Api and getting back the response from the server.

package com.restAssured;

import io.restassured.RestAssured;
import static io.restassured.RestAssured.*;

public class RestServiceDemo {

public static void main(String[] args) {
		
//=====================Post Rest Api======================		
RestAssured.baseURI="https://rahulshettyacademy.com";
RestAssured.useRelaxedHTTPSValidation();
		
given().queryParam("key", "qaclick123").header("Content-Type", "application/json")
.body("{\r\n" + 
"  \"location\": {\r\n" + 
"    \"lat\": -38.383494,\r\n" + 
"    \"lng\": 33.427362\r\n" + 
"  },\r\n" + 
"  \"accuracy\": 50,\r\n" + 
"  \"name\": \"Noida Residence\",\r\n" + 
"  \"phone_number\": \"(+91) 8587657896 \",\r\n" + 
"  \"address\": \"29, Supertech, cohen 09\",\r\n" + 
"  \"types\": [\r\n" + 
"    \"shoe park\",\r\n" + 
"    \"shop\"\r\n" + 
"  ],\r\n" + 
"  \"website\": \"http://google.com\",\r\n" + 
"  \"language\": \"French-IN\"\r\n" + 
"}\r\n" + 
"")
.when().post("/maps/api/place/add/json")
.then().log().all().assertThat().statusCode(200);	

	}

}

Code in Eclipse:

Test Rest Api using Rest Assured

Code Explanation:

1) This line of code uses Rest Assured Class followed by baseURI method to capture the baseURL

RestAssured.baseURI="https://rahulshettyacademy.com";

2) It means you are trusting all hosts even regardless the SSL certificate is invalid

RestAssured.useRelaxedHTTPSValidation();

3) In this line of code we are providing the credentials, as query parameters header type and request json under body keyword.

given().queryParam("key", "qaclick123").header("Content-Type", "application/json")
.body("{\r\n" + 
"  \"location\": {\r\n" + 
"    \"lat\": -38.383494,\r\n" + 
"    \"lng\": 33.427362\r\n" + 
"  },\r\n" + 
"  \"accuracy\": 50,\r\n" + 
"  \"name\": \"Noida Residence\",\r\n" + 
"  \"phone_number\": \"(+91) 8587657896 \",\r\n" + 
"  \"address\": \"29, Supertech, cohen 09\",\r\n" + 
"  \"types\": [\r\n" + 
"    \"shoe park\",\r\n" + 
"    \"shop\"\r\n" + 
"  ],\r\n" + 
"  \"website\": \"http://google.com\",\r\n" + 
"  \"language\": \"French-IN\"\r\n" + 
"}\r\n" + 
"")

4) under When keyword we are providing the resource or in other words the extended URL after base url. for ex: https://rahulshettyacademy.com//maps/api/place/add/json

.when().post("/maps/api/place/add/json")

5) Under then keyword, we provide validation code here we provided 200 which means successfull

.then().log().all().assertThat().statusCode(200);

So in above tutorial we have learned the test rest assured api using rest assured, in further tutorial we will learn more features with in Rest API