Getting started with REST-Assured
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:
- What is REST-Assured?
- How does REST-Assured work?
- Pre-Requisites for Creating a REST-Assured Project
- Creating a REST-Assured Project
- Conclusion
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:
- GIVEN
- WHEN
- THEN
How does REST Assured work?
- Creates an HTTP request
- Send the request over to the server
- 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:
- Open Eclipse and create a new Java Project (File -> New -> Java Project).
2. Convert the project to a Maven project.
(Right Click on the project -> Configure -> Convert to Maven Project -> Enter Group id ->Artifact id-> Finish)
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:
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:
Validation of API Responses:
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: