Getting started with REST-Assured

Shubham Pandey
Version 1
Published in
4 min readMar 31, 2023

As we all know that the demand for API is increasing day by day, so it is important to have knowledge of API testing and how it works. There are multiple types of APIs such as REST, SOAP, etc. For this article, I will be focusing on REST API using REST Assured.

In this article I will be explaining the below topics:

  1. What is REST-Assured?
  2. How does REST-Assured work?
  3. Pre-Requisites for Creating a REST-Assured Project
  4. Creating a REST-Assured Project
  5. Conclusion
working with APIs

What is Rest Assured?

Rest Assured is a Java-based library that is used to test RESTful Web Services/APIs. It can be easily integrated with Maven. It also provides different types of assertions which can be used to validate body, headers, status codes, response time, etc…
REST Assured works on 3 Principles:

  1. GIVEN
  2. WHEN
  3. THEN

How does REST Assured work?

  1. Creates an HTTP request
  2. Send the request over to the server
  3. Validate the response

Prerequisites for creating a REST Assured project

1. Download Java by clicking on the link.

2. Configure the Java path under the system variable as shown in this article.

3. Install any IDE (i.e Eclipse, IntelliJ). In this article, I am using Eclipse as an IDE. You can download Eclipse here.

Creating a REST Assured Project:

  1. Open Eclipse and create a new Java Project (File -> New -> Java Project).
creating new project in eclipse

2. Convert the project to a Maven project.

(Right Click on the project -> Configure -> Convert to Maven Project -> Enter Group id ->Artifact id-> Finish)

Converting the project to Maven Project

3. Open the pom.xml file and add the below dependency:

<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.4.0</version>
</dependency>

Using the above dependency REST Assured library will be available in our project.

4. Once the RESTAssured will be added to the project we need to apply some assertions to validate the responses we get. For that we need to add TestNG to our project:

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
</dependency>

5. For more assertion we will be installing Hamcrest

Hamcrest provides the feature to add some more assertions into the code.

<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
</dependency>

Add all these dependencies to pom.xml file as below:

pom.xml file

After completing this step, we are done with the Rest-Assured setup. Now we can write REST Assured test Cases.

6. Create a new class for writing REST Assured code. (Right-click on src folder -> New -> class)

7. Write test cases to automate the request.

As mentioned above REST Assured works on GIVEN, WHEN, and THEN statements.

1. GIVEN() — We Provide all the input details (Base URI, Headers, Path Parameter, and Query Parameter)

2. WHEN() — We specify request methods like POST, GET, PUT, Patch, or DELETE.

3. THEN() — We validate the response code, response time, etc…

Code for sending the request with body:

POST request

Validation of API Responses:

validation

Conclusion:

Here I have explained how REST Assured works. It is very useful in creating powerful automated tests for RESTful APIs. It also has a lot of assertions which will help us to validate the application at an early stage.

References:

https://rest-assured.io/

--

--