How to do API testing Using Rest assured?
Tags: webdev, programming, java, beginners
Published: 2022-11-04 13:16:46

**
**What Is Rest API?**
Rest stands for Representational state transfer. It’s an architectural style that defines a set of rules in order to create a web service. In a client-server communication, rest suggests creating an object of the data requested by the client and sending the values of the object in response to the user.

**What Is Rest Assured?**
Rest Assured is a java- based library that is used to test Restful web services/Apis

**3 Simple Steps to setup RestAssured project:
**
Install java and set in system variables
Install eclipse and create java Maven Project
Configure Rest assured jar into the project
Getting Started with Framework Creation
In order to create the framework first we need to set up an IDE, install java and then create a maven project.

**Steps to create a Maven Project:
**
Go to eclipse > navigate to File > new > Project > select maven project > Next >Provide group & Artifactid to create a project

Steps to install TestNG in maven project: Go to help in eclipse> go to eclipse marketplace> now search TestNg and install TestNG in maven project

**Adding The Required Dependencies in the Project
**<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>5.2.0</version>
</dependency><!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.6.1</version>
    <scope>test</scope>
</dependency><!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest -->
<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest</artifactId>
    <version>2.2</version>
</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>
Selecting the Appropriate Method

Here is the HTTP method that corresponds to these actions :

GET Method: Read

Post Method: Create

Let’s see an example of GET and Post Request using Rest Assured :

Request Type GET:

After creating a TestNG Class let’s write the test script:

Step :1 Use the rest-assured class to generate a request specification for the URL
Step: 2 Specify the HTTP method for getting a request
Step:3 Use RestAssured.given() and HTTP method for sending the request to the server
Step:4 Response object will send the request using GET Method

Execute test script and response body is shown in the below picture:


Request Type Post:

After creating a TestNG Class let’s write the test script:

Step :1 Use the rest assured class to generate a request specification for the URL
Step: 2 In the post method we need to send the request using the Request Object
Step:3 In post request we need to send a parameter for creating the new entry
Step:4 Body in JSON format hence we are using JSON Object
Step:5 Request parameter we need to send it json.put
Step:6 For Post request we need to add header Request.header(“Content-Type”,” application/json”);
Step:7 Response Object will send the request using the Post method