What is Rest Assured?
Rest Assured is an open-source Java library to test and validate the REST APIs. It can be used to test JSON and XML-based web services. Rest Assured can be integrated with JUnit and TestNG framework for writing test cases.
Rest Assured is a widely used open-source Java library that has become the go-to tool for testing and validating REST APIs. RESTful APIs have become an essential part of modern web applications, and ensuring they function correctly is critical to maintaining high-quality software. Rest Assured provides an easy-to-use and efficient API testing framework that helps ensure that APIs are functioning as intended and delivering the expected results.
Rest Assured supports HTTP requests, including POST, GET, PUT, DELETE, OPTION, PATCH, and HEAD. It can be used to validate and verify the response to these requests.
- Supports JSON schema validator, JSON path, and XML path to parse and verify the response of the APIs
- Supports multi-part form data and various authentication mechanisms
- Supports data-driven testing
- Support logging mechanisms for both request and response details
Advantages of Rest Assured
- It is an open-source Java library
- The initial setup is straightforward
- It follows the BDD approach and uses keywords like given()when(), and then(), which make the code readable and support clean coding
- It has inbuilt methods for the quick assertion of status code, response time, etc.
- Headers, cookies, Content-Type, and other request parameters can be verified on the fly
- Very rich in the ready-made assertion
- The response is given in JSON or XML, so it is easy to parse and validate
- It can be easily integrated with testing frameworks like JUnit, TestNG, etc.
- Support JSONPath and XMLPath to parse the JSON or XML response
Disadvantages of Rest Assured
- Rest Assured does not support the testing of SOAP (Simple Object Access Protocol) APIs explicitly
- Requires good Java programming knowledge
Why is Rest Assured better than Postman for automating RESTful APIs?
- Pricing: Rest Assured is an open-source Java library, whereas Postman is a limited open-source tool
- Code Reusability: Code reusability is possible in Rest Assured as it is a Java client, but in Postman, it is not possible; one needs to copy-paste code or requests from one collection to another
- Customize Reports: Rest Assured allows to customize reports using Extent Report or Allure Report, but in Postman report, customization is not possible
- Designing a Data-Driven Framework: In Postman, we can use only one data file for each collection, but in Rest Assured, there is no such limitation
Rest Assured Flowchart:
Prerequisites for testing APIs using Rest Assured
- IDE (Eclipse, IntelliJ, etc.)
Steps to create a project for API testing
- In any IDE (Eclipse, IntelliJ), create a Maven project
- Add maven dependencies to the pom.xml file
- Create test scripts for employees using API (Create, Get, Update, and Delete)
- Run the project as a TestNG Suite
Required Maven Dependencies
We validate HTTP methods (POST, GET, PUT, and DELETE) as part of API testing.
A brief introduction to HTTP methods (POST, GET, PUST, PATCH, DELETE)
- GET: Used to retrieve and request data from specified resources on a server
- POST: POST is an HTTP method that is used to create a new resource on the server
- PUT: PUT method is similar to the POST method, and it is used to create or update a resource on the server
- DELETE: Used to delete the resource from the server
The following are the APIs from reqres.in that we used to write end-to-end tests for API automation.
- POST: Create Employee
- Get: Retrieve Employee
- PUT: Update Employee
- DELETE: Delete Employee
- Create a new employee using the POST HTTP and "Create Employee" API.
- Assert the response status code and status line of "Create Employee."
- Fetch the response body parameters and print them in the console.
- Retrieve details of any specific employee by sending the employee ID in the GET "Retrieve Employee" API.
- Assert the response parameters (status, message), status code, and status line and fetch the response body parameters.
- Update details of any specific employee using the UPDATE HTTP method and "Update Employee" API.
- Assert response parameters (status, message), status code, and status line.
- Delete employees using the DELETE HTTP method and "Delete Employee" API.
- Assert the status code and status line and fetch the response body parameters.
POST Request for Create Employee API
Raw JSON Response of the Create Employee API:
The Pretty Print JSON Response of the Create Employee API:
Response Parameters of the Create Employee API Printed in Console:
GET request for Retrieve Employee API:
Raw JSON Response of Retrieve Employee API:
The Pretty Print JSON Response of Retrieve Employee API:
Response Parameters of Retrieve Employee API Printed in Console:
PUT Request for Update Employee API::
Raw JSON Response of Update Employee API:
The Pretty Print JSON Response of Update Employee API:
Response Parameters of Update Employee API Printed in Console:
DELETE Request for Delete Employee API:
Raw JSON Response of Delete Employee API:
Pretty Print JSON Response of Delete Employee API:
Response Parameters of Delete Employee API Printed in Console:
Explanation of Code
- Response: This is an interface used to store the response of the REST request
- given() is a BDD-style method that contains the request specification (like the header, cookies, body, authentication, etc.)
- when(): when method contains the action, that is, HTTP method to send the request (post(), get(), put(), delete())
- then(): then method contains the validation of the sent request, and in then() method, we can verify the response parameters like status code, status line, and response body parameters
- Using resp.asString(), we can print raw JSON response
- using "resp.body().jsonPath().prettyPrint();" we can print the readable JSON response
- JsonPath: JsonPath is a class in Rest Assured used to fetch the response body parameter from an object document; it accepts resp as an argument
Rest Assured is a highly effective and user-friendly open-source Java library designed to create powerful and maintainable tests for RESTful APIs. It offers a BDD-style approach to coding and maintaining API test cases, which makes it easier for testers to understand and implement. Furthermore, it provides a wide range of ready-made assertions and inbuilt methods that simplify the process of creating test cases and ensures that the results are accurate and reliable.
Overall, using Rest Assured can greatly improve the efficiency and accuracy of API testing while minimizing the effort and time required to create and maintain test cases. Whether an experienced developer or a novice tester, this powerful tool can help you achieve your testing goals more effectively and efficiently.