Rest Assured is a Java-based library that can be integrated with JUnit and TestNG frameworks and help us in automating the testing of RESTful API. It is helpful to write Unit tests as well as implementing automation frameworks.
It is considered to be the wrapper implementation of the HTTP client library as it underneath performs the same operations as what HTTP client API does. It supports POST, GET, PUT, DELETE, OPTIONS, PATCH, and HEAD requests and can be used to validate and verify the response of these requests.
It has so many useful functions that can be used to send all types of HTTP methods, accessing public as well as private API by fulfilling authentication.
It is highly scalable and effective to create and send Requests to tests a particular service to the server and the response data is further validated against the expected data.
REST Assured can be used in combination with existing unit testing frameworks, such as JUnit and TestNG. With REST Assured 4.1.2 is released, adds support for Java 13.
Benefits of Rest Assured
- It eliminates the demand to set up an HTTP connection, send a request and receive and parse a response
- It supports GWT(Given, When, Then) annotations which make the test very reliable
- It can be easily integrated with other Java libraries like TestNG, Junit for Test Framework and Extent Report, Allure Report for reporting purposes.
- It can be run as a part of CI/CD framework
- It supports many commonly used authentication mechanism
- It provides the feasibility to validate the response such as header, status code, etc.
- It helps to verify response by putting assert statements and conditions.
- It requires less coding compare to Apache Http Client.
- It Supports JsonPath and XmlPath which helps in parsing JSON and XML response. Rest Assured by default integrates both.
- Can be used to verify JSON Schema using the JSON Schema Validation library.
Objective of Testing with Rest Assured
The first target is to cover the functional testing using Rest Assure by ensuring that the API functions correctly. Below are some of the main objectives of Testing with Rest Assured
- To ensure that the implementation is working correctly as expected without any bugs!
- To ensure that the implementation is working as specified according to the requirements specification (which later on becomes our API documentation).
- To prevent regressions between code merges and releases.
Rest Assured Test Action Points:
Each test is comprised of test actions. To test each API requests, the following actions need to be considered:
- Verify HTTP code status: Test conditions like when a new record is added response should be return with status 201 OK and any unpermitted requests should return 403 FORBIDDEN, etc.
- Verify response payload: Verify the response body, field names, types and data, and also error responses.
- Verify response headers: Verify headers of each API response as this has an implication on both security and performance.
- Verify basic performance: Check for the response time, if the operation was completed successfully but took an unreasonable amount of time, then the test should fail.
Possible Test scenarios with Rest Assured:
- Positive tests – To check the basic functionality and acceptance criteria of API.
- Positive Tests with optional parameters – To include optional parameters and extra functionality in tests.
- Negative Tests with valid input – Test negative scenarios with valid inputs like trying to add existing username
- Negative Tests with invalid input – This include testing the scenario like trying to add a username which is null
- Destructive testing – This a deeper form of negative testing where an intentional attempt to break the API is made to check its robustness