QA Automation

API Automation Testing Using REST-assured with Java  

Blogs (4)

Introduction: 

REST-assured enables you to test REST APIs using java libraries. It is very efficient and easy to use. Integration with maven is also easy. REST-assured has methods to fetch data from every request and response part. 

It can be used to test XML & JSON-based web services, however, the structure is complex. 

Pre-requisite: 

  1. JDK should be installed in the system.
  2. IDE for running and executing java code (either eclipse or other). 

Installations:  

Firstly, you must create a new maven project in your eclipse IDE. Then we need to add a dependency for rest assured and testNG to execute code for API testing. 

The below dependency is for REST-assured added to your pom.xml file in your maven project.

REST-assured-dependecy

Apart from that, you need to add TestNG framework to your maven project. For that, you need to add a dependency for TestNG. 

REST-assured-TestNG

Then, you need to go to src/test/java in your maven project and you can either create a new java class or write code in the default class. 

Syntax:  

The syntax of REST-assured is easy and similar to the BDD  structure. It uses given/when/then, therefore it is easy to understand and readable. 

REST-assured-Syntax

Create [POST]:

To create data using REST-assured we use post() method. Firstly, we should have data in the form of JSON Object and authorization tokens as needed. Using TestNG as testing framework we can post any request as defined in the code below: 

By getting status code as 201. That means your data is created successfully. 

REST-assured-create

Read [GET]:

To read the data using get() request in REST-assured with TestNG framework use the code below. 

REST-assured-read

Update [PUT]:

To update data using REST-assured we use put() method. Firstly, we should have data in JSON Object and authorization tokens as needed. Using TestNG as a testing framework we can update any data as defined in the code below: 

By getting the status code as 200. That means your data is updated successfully. 

Update

Delete [DELETE]:

To DELETE the data using REST-assured and get response code as 204. The code for DELETE request is below. 

Delete

Pros: 

  1. It is easy to use since it uses given/when/then test notations which are readable easily. 
  2. Easy to integrate with frameworks like TestNG and JUnit. 
  3. Code reusability is excellent as it is a Java client. 
  4. CI/CD integration is easy as we can easily integrate with tools like Maven and Jenkins. 
  5. REST-assured can be used with customized and open-source reporting tools.
  6. It provides DSL so the test is Behaviour driven. 
  7. It provides an easy interface, which is why it is a better tool rather than other complex tools. 

Cons: 

  1. Does not support SOAP APIs explicitly. 
  2. There is no inbuilt reporting in REST-assured. 
  3. Since it uses Java as a programming language thus requires good knowledge of Java. 

Write A Comment