
Rest Assured API Automation Testing Library
Rest-Assured is a Java-based library that is used to test RESTful Web Services. This library behaves like a headless Client to access REST web services. It's simple Java libraries are used for testing the Rest Web Service API. Also provides the ability to validate the Rest-HTTP Responses received from the server. For example, we can verify the Status code, Status message, Headers, and even the Body of the response. This makes Rest-Assured a very flexible library that can be used for testing. It supports both XML and JSON, JS, HTML, and text.

Advantages of Rest-Assured
- Support for HTTP methods
- Support for BDD ()
- Use of Hamcrest matches for validation /Assertions
- Provide inbuilt method to create and validate a request and response header & body
- Handles various authentication like Token AUTH, Basic auth, oauth1.0, oauth2.0
- Send request over the network using existing protocol like http
- Integrate seamlessly with existing Java-based frameworks like Selenium, testing, JDBC, etc.
- Used for complete backend automation
- Framework can integrate with CI/CD pipeline
- Open-source headless client.
Rest Assured Required libraries
Tool | Description |
---|---|
rest-assured-3.0.2 | Rest api headless client |
json-simple-1.1.1 | Used to create json object |
jackson-mapper-asl-1.9.13 | Used to create pojo object & perser |
testng-6.8 | Unit testing tool for batch execution reports |
Ways to post a Request in Rest-Assured.
- Using JSON Object class
- Using Hash MAP
- Using POJO class
- Using JSON File
Rest Assured CRUD Operation
In real-time, Every API should perform CRUD operation before exposure. CRUD is the basic operation where we can test the Rest API, whether we can perform Read, Write, Update and delete Operations.
Read / Get
To get all the resources from the server, we go for “get” HTTP method.
@Test
public void getAllProjects() {
when()
.get(“http://localhost:8080”)
.then()
.assertThat().statusCode(200)
.and()
.assertThat().contentType(ContentType.JSON)
.log().all();
}
Create / POST
To create the resource inside the server, we go for the “Post” HTTP method.
@Test
public void createProject() {
JSONObject jObject= new JSONObject();
jObject.put(“projectName”, “ola”);
jObject.put(“status”, “Created”);
jObject.put(“teamSize”, “10”);
given()
.contentType(ContentType.JSON)
.body(jObject)
.when()
.post(“http://localhost:8084/addProject”)
.then()
.log().all()
.assertThat().statusCode(201)
.assertThat().contentType(ContentType.JSON);
}
Update/ PUT
To create/update the exiting resource inside the server, we go for “Put” http method.
@Test
public void updateProject() {
JSONObject jObject= new JSONObject();
jObject.put(“projectName”, “ola”);
jObject.put(“status”, “Created”);
jObject.put(“teamSize”, “10”);
given()
.contentType(ContentType.JSON)
.body(jObject)
.when()
.put(“http://localhost:8084”)
.then()
.log().all()
.assertThat().statusCode(201)
.assertThat().contentType(ContentType.JSON);
}
Partial Update/ PATCH
To partially update the exiting resource inside the server, we go for “Patch” HTTP method.
@Test
public void updateProject() {
JSONObject jObject= new JSONObject();
jObject.put(“projectName”, “uber”);
given()
.contentType(ContentType.JSON)
.body(jObject)
.when()
.patch(“http://localhost:8084”)
.then()
.log().all()
.assertThat().statusCode(201)
.assertThat().contentType(ContentType.JSON);
}
Delete / delete()
To delete the exiting resource inside the server, we go for “delete” http method.
@Test
public void getAllProjects() {
when()
.delete(“http://localhost:8080”)
.then()
.log().all()
.assertThat().statusCode(204);
}
Conclusion: Key Takeaways on Rest Assured API Testing Library
Since API Testing is a critical part of any development life cycle, REST Assured Framework is one of the most widely used Web Services Testing tools in Java. Advanced features, along with simplicity in implementation, make it a must for any testers to ensure quality in the end product. With its fluent approach and the expressive method names, it makes the call and its output easily understandable. Both JSONPath and Matchers further increase the power and expressiveness.
Read more blogs here