Simple way to perform REST API Automation with Rest Assured, BDD, Java

Balaji Ganesh
2 min readAug 25, 2020

In this tutorial, let see how to quickly setup a REST api automation framework with Rest Assured and Cucumber 6.

To setup a cucumber project refer the link below :

Assuming, you have setup the BDD project, your project should look something like below. Additionally create a folder called requests under the resources directory :

├── pom.xml
└── src
└── test
├── java
└── StepDefs
└── resources
└── features
└── requests

Maven Dependencies :

  1. Rest Assured : Testing and validating REST services in Java is harder than in dynamic languages such as Ruby and Groovy. REST Assured brings the simplicity of using these languages into the Java domain.
  2. Rest Assured JSON Path : This is used to read the JSON response returned by Rest Assured. This plugin is based on groovy language.
  3. Handlebars : Handlebars provides the power necessary to let you build semantic templates effectively with no frustration. Here, we are going to build our request using handlebars.

Below are the dependancies that you may have to add into your pom.xml :

<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.1.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-path</artifactId>
<version>4.1.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>2.5.8</version>
</dependency>
<dependency>
<groupId>com.github.jknack</groupId>
<artifactId>handlebars</artifactId>
<version>4.1.0</version>
</dependency>

My API details are as follows :

URL : http://localhost:3000/loginRequest Body : 
{
"username" : "",
"password" : ""
}
Expected Response :
{
"token" : "",
"user" : {
"role" : "",
"email" : ""
}
}

Now that we have our dependacies added lets write our feature file and step definition for the above request :

Create a new file called “login.json” (in my case) under “src/test/resources/ request” directory and parameterize the values based on your need like below :

{
"username" : "{{username}}",
"password" : "{{password}}"
}

Lets now prepare the rest processor, copy the following code into a class class called Rest Processor :

This is a single class which will process GET, POST, PUT, DELETE rest methods.

You can alternatively clone my git repository from the link below, which contains all the above mentioned code along with the integration for Retry Listeners and Extent Report.

--

--