Let’s get started with ‘What is Rest assured’:
Rest Assured is a Java library or Java API for testing RESTful webservices
It can be used to test XML & JSON based webservices
It supports different http methods like POST, GET, PUT, DELETE, OPTIONS, PATCH, HEAD and so on.
It can be integrated with any testing frameworks like JUNIT, TestNG etc.
Rest Assured is implemented in Groovy
Using Rest Assured we can create tests using BDD (Behavior Driven Development), keywords like Given, when, then.
Rest Assured enables us to test REST APIs using java libraries and integrates well with Maven. It has very efficient matching techniques, so asserting our expected results is also pretty straight forward.
Before we start creating a rest assured project, we will have to keep in mind the pre-requisite which we will need for testing API’s.
Prerequisites before we start creating a rest assured project:
We should be having Java on our system.
We need an IDE, like Eclipse
We are going to create a Maven project, so we will need Maven
TestNG for creating a testing project or testing framework with rest assured
How to create a project in Rest Assured for API testing:
I would recommend to go to the official website of Rest Assured (rest-assured.io), and read all the documentation part. There are many important information available to get benefited from.
Now, We will start with creating project for API Testing:
Open Eclipse
Click on File > New > Other
Create a Maven Project
I have named the Maven Project as RestAssuredDemo
We will put the same name in fields Group Id, Artifact Id, Name, Description.
We have to click on Finish button, and the Maven Project named RestAssuredDemo will be created.
Add Dependencies in pom.xml
We will have to add dependencies to the pom.xml file in the RestAssuredDemo Project. We can download all the dependencies we require for rest assured from ‘Maven Central Repository’. It’s simple, you will have to search for dependencies in mvnrepository.com. So, some of the dependencies we need to search and download from the repository are:
Rest Assured
TestNG
JSON path
Here is my dependencies code file for your reference:
<dependencies>
<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.3.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.1.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
After adding the dependencies in pom.xml, we will have to save the file and then go to ‘Project’ tab and click on ‘Build All’. After this step we will have a folder called Maven Dependencies in our ‘RestAssuredDemo’ Project. Maven project makes it easy to maintain the library and the JAR files. If we are not using maven project, then we will have to download all the Jar files manually, and also check for any new update / version from time to time. So, Maven makes the library management super easy.
Now after the Rest Assured installation, we will create a class inside ‘src/test/java’. We will right click on src/test/java and follow the instruction given in the image.
For example: First, we will create a class to test GET request:
Clicking on Finish will create a class named 'Test_Get'.
This is all about installing Rest Assured and getting started with API testing. In the next blog I will put forward all the details for Testing an API using rest assured and seeing how GET, POST, Delete and PUT method works.