REST Assured API testing

Chaya Thilakumara
Chaya Thilakumara
Published in
3 min readMay 11, 2020

-With sample requests and tests for GET, POST , PUT, PATCH, DELETE

Rest assured is java library for testing Restful Web services. It can be used to test XML & JSON based web services. It supports GET, POST, PUT, PATCH, DELETE, OPTIONS and HEAD requests and can be used to validate and verify the response of these requests. Also it can be integrated with testing frameworks like JUnit, TestNG etc.

Prerequisites : Java, IDE (Eclipse, IntelliJ, etc), Maven & TestNG

Steps to create project for API Testing :First create a maven project & add dependencies in pom.xml. Then create your test script, verify & run it.

Configure TestNG & REST Assured

After creating your maven project add dependencies in pom.xml.

Maven dependency for rest assured 
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.3.0</version>
<scope>test</scope>
</dependency>
Maven dependency for testng
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.1.0</version>
<scope>test</scope>
</dependency>

Let’s see some sample requests and tests for GET, POST , PUT, PATCH & DELETE. But before that let’s see what are these methods GET, POST , PUT, PATCH & DELETE.

Methods

HTTP methods (GET, PUT, POST, PATCH and DELETE) and these methods can be mapped to CRUD operations.

  • GET retrieves the resource at a specified URI.
  • PUT updates a resource at a specified URI. Also be used to create a new resource at a specified URI. Replaces the entire product entity.
  • PATCH support partial updates.
  • POST creates a new resource.
  • DELETE deletes a resource at a specified URI.

GET : Request (Response : 200)

Response : https://reqres.in/api/users?page=2
GET Request -1
GET Request -2
Test Results

POST : Request (Response : 201)

JSON library (Other libraries : gson,jackson,json,Simple json)
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
Response : https://reqres.in/api/users
POST Request -1
Test Results

HTTP response status codes are grouped in five classes:

  1. Informational responses (100199),
  2. Successful responses (200299),
  3. Redirects (300399),
  4. Client errors (400499),
  5. Server errors (500599)
200 : OK : Request has succeeded201 : Created : Request has succeeded and a new resource has been created

PUT : Request (Response : 200)

Response : https://reqres.in/api/users/2
PUT Request -1
Test Results

PATCH : Request (Response : 200)

Response : https://reqres.in/api/users/2
PATCH Request -1
Test Results

DELETE : Request (Response : 204)

Response : https://reqres.in/api/users/2
DELETE Request -1
Test Results

I think,this article would help you to get a simple start in API testing with REST Assured.

Let’s meet with another exciting article in a series.

TILL THEN HAPPY TESTING!!!

Reference: Automation Step by Step — Raghav Pal

--

--

Chaya Thilakumara
Chaya Thilakumara

Pursue your passion, and everything else will fall into place.