Software testing is an integral part of an application’s maintenance and quality control. The better testing processes you have, the better customer experience you will be able to provide.
The concept of software testing was introduced by Charles Baker in 1957, stating the need to design tests to ensure that the developed system meets the requirements set forth before the development started.
Before that, testing was included in the debugging phase, wherein programmers found errors during debugging. Gradually, software testing became an indicator of quality and it gained its position as a separate and definite process of the software development lifecycle.
Suffice to say, software testing techniques have evolved since then. From manual testing to methodologies like agile testing, exploratory testing, white box testing, black box testing and now automated testing techniques, the IT industry and quality analysts have transformed the way testing works.
In this blog, we will understand Rest API software testing techniques and the automation applied behind them.
Table of Contents
- Introduction to REST API Testing
- What is REST Assured?
- What are the Uses of Rest Assured?
- How to Create the First Project for API Testing?
- How to Automate Rest API with Rest Assured?
Let’s start with the basics.
Introduction to REST API testing
REST API Testing is an open source web automation software testing technique used to test RESTful APIs for web applications. The purpose of REST API testing is to record and check the API’s response and functioning by sending various HTTP/S requests. The testing is done by GET, POST, PUT and DELETE methods.
Standing for Representational State Transfer, REST is an architectural style and not a protocol. Whereas, API stands for Application Programming Interface; which is a set of programming instructions to access a web based software application.
In other words, an API is a set of commands used by an individual program to communicate with another program and use each other’s functions and databases to get information.
For example, a MakeMyTrip website can have an API for various functions like search, calendars, sorting results, filtering results as per conditions and more.
The REST API software testing technique uses four types of methods, similar to a majority of other techniques: GET, POST, DELETE and PUT.
- GET: The GET method is used to extract information from the server using a given URI. Apart from receiving data, a GET request should have no other effect on the data.
- POST: A POST request is used to create a new entity. In addition, it is used to send data to the server, such as customer information, uploaded files, and more using HTML forms.
- DELETE: The DELETE request removes all current representations of the target resource pointed by a URI.
- PUT: This creates a new entity or updates an existing one.
REST API can be tested manually as well as automatically.
Which Tools are Used to Test REST API Manually?
Various tools and systems are put into practice to test the REST API manually. Some of those are:
- PostMan Plugin: Install from here.
- RestLet Client: https://restlet.com
- Soap UI and many more
- Selenium for automating the Rest API’s: https://www.eclipse.org/downloads/
What is REST Assured?
Rest Assured is an open-source Java Domain Specific language (DSL). A JAVA library (API), Rest Assured is used for testing RestFul web services while eliminating the need for tedious coding to test complex responses. Implemented in groovy, Rest Assured supports both XML and JSON format.
Moreover, it supports HTTP requests such as GET, PUT, DELETE, POST, PATCH, OPTIONS, HEAD which can further be integrated with testing frameworks like JUnit, TestNG.
REST Assured Dependencies
Let us see what are the dependencies of Rest Assured.
First off, you will need to add Rest Assured as a dependency to the project you want to test. Because we use maven in our projects, we have added the following REST-assured dependency to our Project Object Model (POM).
Dependencies will be directed to this link and search for Rest Assured and TestNG
- REST Assured :
<dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <version>4.4.0</version> <scope>test</scope> </dependency>
- TESTNG:
<dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.4.0</version> <scope>test</scope> </dependency>
- JSON Path:
<dependency> <groupId>io.rest-assured</groupId> <artifactId>json-path</artifactId> <version>4.4.0</version> <scope>test</scope> </dependency>
- XML Path:
<dependency> <groupId>io.rest-assured</groupId> <artifactId>xml-path</artifactId> <version>4.4.0</version> <scope>test</scope> </dependency>
What are the Pre-Requisites to Use Rest Assured?
Rest Assured requires a few criteria to be met before being implemented. Those are:
- JAVA version 8
- Eclipse (Install from here)
- Maven (Already built in capabilities in all the versions of Eclipse)
- TestNG (To be downloaded from the Eclipse)
What are the uses of REST Assured?
Rest Assured comes with a multitude of benefits and uses in software testing techniques, such as:
- It has architectural style protocol
- Uses XML or JSON to send and receive data
- Testing for core functionalities
- Time effective quick response time
- Language independent
- Easy integration with GUI
- Simply calls services via URL path
- Result is readable plain XML or JSON
How to Create the First Project for API Testing?
Follow the below steps to create the first project to test the REST API.
- Step 1: Open Eclipse
- Step 2: Create a Maven project following the below steps:
2.1 Right click on the Package Explorer or Click on File
2.2 Click on New -> Click on Other -> Search for Maven Project -> Select the Maven project and click on Next
2.3 Check the Checkbox of Create a Simple project and click on Next
2.4 Enter the Group Id and Artifact Id as Project name and click on Finish
- Step 3: Add dependencies in pom.xml
How to Automate Rest API with Rest Assured?
Rest API can be tested manually as well as using automation. You can automate it using Rest Assured with the following steps.
- To get started with the first Test for GET Method using Java
- Rest API for Testing to test all the HTTP Methods
package API_Demo; import org.testng.Assert; import org.testng.annotations.Test; import static io.restassured.RestAssured.*; import io.restassured.response.Response; import static io.restassured.matcher.RestAssuredMatchers.*; import static org.hamcrest.Matchers.*; public class RestAssured_Test_01 { @Test void test01_Get() { Response response = get("https://reqres.in/api/users/2"); System.out.println(response.getStatusCode()); System.out.println(response.getContentType()); System.out.println(response.getBody()); System.out.println(response.getStatusLine()); System.out.println(response.getTime()); int Statuscode= response.getStatusCode(); Assert.assertEquals(Statuscode, 200); }
- API Testing for POST Method Program using JAVA
package API_Demo; import java.util.HashMap; import java.util.Map; import org.apache.http.entity.ContentType; import org.json.simple.JSONObject; import org.testng.annotations.Test; import groovy.util.logging.Log; import static io.restassured.RestAssured.*; public class Tests_POST { @Test public void test_01_post() { Map<String,Object> map = new HashMap<String,Object>(); map.put("name", "Rupali"); map.put("job", "Teacher"); JSONObject request = new JSONObject(map); System.out.println(request); given(). header("Content-Type", "application/json"). body(request.toJSONString()). when(). post("https://reqres.in/api/users"). then(). statusCode(201).log().all(); } }
- Console
In Conclusion
Testing in software engineering, whether white box or black box techniques have made their way around application development over the last few decades.
Instead of sticking around as a process or a chore, software testing has become a core competency in the IT industry.
We hope this blog helped you understand REST API and how to test it using Rest Assured. Leave your feedback or questions in the comments below.