API Testing with REST Assured-I

Malsha Nishadini
4 min readJan 7, 2020

--

APIs are playing a major role in most software applications. Therefore proper automated API testing is becoming more important and essential in these days. Commonly API testing includes testing REST APIs or SOAP web services. In this article, I’m going to show you how to use REST Assured for testing RESTful APIs.

In here, I’m going to write REST Assured test which is made a call to a web service. You can simply start with REST Assured in combination with Maven and TestNG. First let’s see how to configure Intellij with REST Assured.

You can add REST Assured as a dependency to the pom.xml as follows.

<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>3.0.2</version>
<scope>test</scope>
</dependency>

All the tests are carried out based on the following simple Restful web service.

Endpoint : http://restapi.demoqa.com/utilities/weather/city/<City>(<City> means the city for which we are trying to retrieve the weather data.)

HTTP method : GET

Ex:

Request: http://restapi.demoqa.com/utilities/weather/city/Colombo

Response:

We can follow below steps to test the above scenario using Rest Assured.

  1. Use the RestAssured class to generate a RequestSpecification for the URL: http://restapi.demoqa.com/utilities/weather/city/Colombo
  2. Specify the HTTP Method type
  3. Send the Request to the Server
  4. Get the Response back from the server
  5. Print the returned Response’s Body

Code:

import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.http.Method;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

public class SimpleGetTest {

@Test
public void GetWeatherDetails()
{
// Define the base URL to the RESTful web service
RestAssured.baseURI = "http://restapi.demoqa.com/utilities/weather/city";

// Get the RequestSpecification of the request that you want to sent to the server using given() method.
// The server is specified by the BaseURI as in the above step.
RequestSpecification httpRequest = RestAssured.given();

// Make a request to the server by specifying the method Type and the method URL.
// This will return the Response from the server. Store the response in a variable.
Response response = httpRequest.request(Method.GET, "/Colombo");

// Print the response body that received from the server.
String responseBody = response.getBody().asString();
System.out.println("Response Body is => " + responseBody);


}

}

The above code will produce the same response which was received when opened the same URL on a browser.

Let’s understand the above code clearly.

Code line 1

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

This line set up a request with the specified base URI using the class called “RestAssured”. In this scenario the base URI is “http://restapi.demoqa.com/utilities/weather/city” . Base URI is the root address of the resource that we are trying to access.

RestAssured class can import as “io.restassured.RestAssured” and helps creating Request of different HTTP method types (GET, POST, PUT, PATH, DELETE, UPDATE, HEAD and OPTIONS) and retrieves response from the server. Also it supports validating the Response received from the server.

Code line 2

RequestSpecification httpRequest = RestAssured.given();

Once the request is set up, Request need to be stored in a variable so that it can be modified. Every Request in Rest-Assured library is represented by an interface called RequestSpecification which allows to modify the request, like adding headers or adding authentication details.

Code line 3

Response response = httpRequest.request(Method.GET, “/Colombo”);

After creating the RequestSpecification, we need to call the server to get the resource. In this code line tells RequestSpecification to issue a request to the remote server and gets a response back. Therefore return type of the request is specified as Response.

In Rest-Assured, Response returned from the server is represented by “io.restassured.response.Response” interface. Response object contains all the data sent by the server and also Different method can be called on the Response object to get different parts of the Response.

Code line 4 & 5

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

System.out.println(“Response Body is => “ + responseBody);

getBody() method which is in the Response interface, will return the body of the received response and printed in the console by converting it into a string value.

Rest-Assuerd supports you to do API testing easily with short methods which help you to write short code. Therefore, your automated API testing will be easy. So, I think,this article will help you to get a simple start in Automated API testing with REST Assured.

Thank you !!!

Read API Testing with REST Assured-II from here…..

--

--