API Automation with the help of REST assured library

Posted By : Jatin Singh | 29-Apr-2020

Loading...

Understanding Rest Architecture

  • REST stands for Representational State Transfer. It is a software architecture that depends on a stateless communications protocol, mostly HTTP.
  • REST structures message data in XML, YAML or any other format that is machine-readable. JSON is now the most popular format being used in REST Services.
  • In REST architecture, a REST Server provides access to various resources and the REST client acquires and presents the various resources. Each resource is identified by URIs/ Global IDs.

URI – Uniform Resource Identifier:

  • A URI is a string of different characters used to identify a resource. Such identification helps to interact with different representations of the resource over a secured network.
  • Each resource is identified by one/multiple URI's. To access the resource, an application calls an HTTP operation(Method) on one of the resource’s URIs.

RESTful services:

  • Web services that are based on REST are called RESTful Web Services.These web services use HTTP methods to implement the concept of REST architecture.
  • A RESTful web service usually defines a URI , which is a service that provides resource representation in JSON format using HTTP Methods.

Role of API in Rest Assured

  • APIs played a major role in recent software developments and hence proper automated testing of these APIs has become essential.There are many different tools that will help you in writing automated tests at the API level.
  • Rest Assured is a Java library that offers a domain-specific language(DSL) to create and maintain tests for RESTful APIs.Integration of Rest Assured with TestNG and JUnit can also be done.
  • To simplify the testing and validation of REST APIs, Rest Assured was developed.
  • One of the most significant features of Rest Assured is that to check the content present in the response, we can make use of the XML path and JSON path. We can parse the response data and test specific elements of their properties using the XML and JSON path.

How to Make a Simple GET call using Rest Assured library

Note: import all the packages mention below:

import io.restassured.RestAssured;

import io.restassured.http.Method;

import io.restassured.response.Response;

import io.restassured.specification.RequestSpecification;

import io.restassured.specification.ResponseSpecification;

public class TC001_GET_Request {

pubic static void main String (args[ ]){

//Specifiy base URI

RestAssured.baseURI = "http://restapi.demoqa.com/utilities/weather/city";

// request object

RequestSpecification httprequest = RestAssured.given();

//Response object

Response response = httprequest.request(Method.GET,"/Delhi");

//print response in console window

// here we are coverting JSON response into String variable

String responseBody = response.getBody().asString();

System.out.println("Response body is: "+responseBody);

//status code validation

int statusCode = response.getStatusCode();

System.out.println("Status code is:"+statusCode);

Assert.assertEquals(statusCode, 200);

//status line verification

String statusLine = response.getStatusLine();

System.out.println("Status line is :" +statusLine);

Assert.assertEquals(statusLine,"HTTP/1.1 200 OK");

}

}


With RESTful API becoming more and more prevalent, accelerating testing with API automation has become an essential component of test plans. We are an ERP development company with expertise in developing customized software solutions for our clients. Our custom development services enable businesses to automate, plan, collaborate, and execute their operations methodically. Get in touch with us to automate API testing for your enterprise software.