Blog Software Testing

REST Assured Overview

Kannathasan Kannathasan | Last updated: June 20, 2024 |

Automating the API Testing has never been an easy task. Right from the initial API Testing setup, sequencing API calls to testing parameter combinations, API Testing as a whole has several challenges. Don’t worry! I have shared an overview of REST API, the REST Assured framework and how the framework will help simplifying API Testing using automation.

What is API?

API stands for Application Programming Interface. It acts as an interface between different software systems and establishes their interaction and data exchange.

What is REST API?

REST means REpresentational State Transfer. REST is an alternative to SOAP (Simple Object Access Protocol) and instead of using XML for request REST uses simple URL.

REST API supports both XML and JSON format. It is usually preferred for Mobile and web apps as it makes app work faster and smoother and the supported HTTP methods are GET, POST, PUT, DELETE.

What is REST Assured?

REST-Assured is a Java-based library that is used to test RESTful Web Services.REST Assured is a Java domain-specific language (DSL) for testing REST services. Supports Response parsing and assertions.

Steps to automate the REST API Get Request

  • Create a maven project on eclipse
  • Add the below dependencies in POM.xml file

<dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>3.0.2</version>
        <scope>test</scope>
</dependency>
<dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.9.10</version>
        <scope>test</scope>
</dependency>
</dependencies>

  • Create a package and class file.
  • Within this class file we need to call the required rest assured methods to call the API and obtain the response code and the message.
  • Use the RestAssured class to generate a RequestSpecification for the URL: http://restapi.demoqa.com/utilities/weather/city/Chennai
  • Mention the HTTP Method type and send the Request to the server
  • Get the Response back from the server
  • Display the returned Response’s Body in Console

Sample Code snippet

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 SimpleGetRequestTest {

	@Test
	public void GetWeatherUpdate()
	{   
		RestAssured.baseURI = "http://restapi.demoqa.com/utilities/weather/city";
                        RequestSpecification httpRequest = RestAssured.given();
		Response response = httpRequest.request(Method.GET, "/Chennai");
		String responseBody = response.getBody().asString();
		System.out.println("Response body for the given request " + responseBody);

	}

}

Output Window

The below response will be displayed in the Eclipse console window for the above request

{
    "City": "Chennai",
    "Temperature": "31.98 Degree celsius",
    "Humidity": "63 Percent",
    "WeatherDescription": "broken clouds",
    "WindSpeed": "4.6 Km per hour",
    "WindDirectionDegree": "130 Degree"
}

 

You would have by now understood what a REST Assured Framework is and how to automate the REST API Get Request along with a sample output. API Testing is an ocean by itself and with the automation disrupting everything, it is important for us to know how to align with the changes.

Having said this, I am ready to hear your thoughts about this blog and any additional insights with regards to Rest Assured Framework. Do post what you think in the comments section below!

Reference

https://www.toolsqa.com/rest-assured-tutorial/
https://devops.com/top-6-challenges-api-testing/

 

 

 

Leave a Reply