How To Make GET Request Using RestAssured Framework
Posted By : Aditi Nahar | 22-May-2018
Today I have a very interesting topic to discuss and that is Performing API Testing using Rest-Assured Framework.
Before proceeding with this topic I assume the readers have good knowledge of TestNG Framework. In this blog, I will discuss briefly Rest-Assured Framework, Set up of Rest-Assured Jars in Eclipse Project and make a GET request using Rest-Assured.
What is Rest-Assured
Rest-Assured is a JAVA library which can be used for writing tests for RESTful APIs. It also has assertions for validating the API responses and forms a good combination with TestNG libraries.
Steps to setup Rest-Assured Jars
1. Download and Setup Java, Eclipse and TestNG.
2. Download Rest-Assured Jars.
3. Create a Project in eclipse.
4. In the classpath of eclipse set Rest-Assured Jars.
After the initial setup is done, let’s now understand how to make GET request with Rest-Assured
Consider the following example:
EndPoint - https://example.com/api/countries/<country>
HTTP method Type - GET
Response -
{
"countryId": 1,
"name": "India,
"capital": "Delhi”,
"states": 29,
"territories": 7,
"phoneCode": "91"
},
Comment - Above response is fetched for the country India.
To fetch this response from Rest-Assured, following steps are to be executed:
1. Using Rest-Assured class,
2. HTTP method is specified.
3. Request
4.
5.
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
public class Example {
@Test
public void listCountries()
{
// Using Rest-Assured class to setup a request
RestAssured.baseURI = "https://example.com/api/countries";
// Getting the RequestSpecification of the request
RequestSpecification httpRequest = RestAssured.given();
// Making GET request directly by RequestSpecification.get() method
Response response = httpRequest.get(“/India");
//Retrieving Body of response
String body = response.getBody().asString();
//Retrieving Status Code of response
int status = response.getStatusCode();
//Retrieving Status Line
String statusLine = response.getStatusLine();
//Printing the response
System.out.println("Response Body is "+body);
System.out.println("Status code is "+status);
System.out.println("Status line is "+statusLine);
}
}
Note:
Above code is written considering positive test scenario but similar can be used for testing negative scenarios also. Try it by yourself and share.
In my next article, I will share about making a POST request using Rest-Assured.
Enjoy Automation Testing :)
About Author
Aditi Nahar
Aditi is a certified QA Engineer with a strong command over management tool sets like JIRA and Trello, as well as QA tool sets for API and performance testing. She possesses excellent verbal and written communication skills and has gained valuable experience in management and leadership while collaborating with clients and large teams. Aditi's ability to apply creative thinking and problem-solving skills makes her adept at handling challenging business scenarios. Her proficiency in manual testing has proven instrumental in identifying issues and ensuring the functionality of applications across web, mobile, and TV platforms. She has made significant contributions to both internal and client projects, including Bits2Btc, AUS-BTC, EZBitex, ACL EAP, Scaffold, Iron Systems VRP, Oremus Zoho, and NOWCAST OTT.