REST Assured API testing
-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)
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>
HTTP response status codes are grouped in five classes:
- Informational responses (
100
–199
), - Successful responses (
200
–299
), - Redirects (
300
–399
), - Client errors (
400
–499
), - Server errors (
500
–599
)
200 : OK : Request has succeeded201 : Created : Request has succeeded and a new resource has been created
PUT : Request (Response : 200)
PATCH : Request (Response : 200)
DELETE : Request (Response : 204)
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