Rest Assured

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

  1. It eliminates the demand to set up an HTTP connection, send a request and receive and parse a response
  2. It supports GWT(Given, When, Then) annotations which make the test very reliable
  3. It can be easily integrated with other Java libraries like TestNG, Junit for Test Framework and Extent Report, Allure Report for reporting purposes.
  4. It can be run as a part of CI/CD framework
  5. It supports many commonly used authentication mechanism
  6. It provides the feasibility to validate the response such as header, status code, etc.
  7. It helps to verify response by putting assert statements and conditions.
  8. It requires less coding compare to Apache Http Client.
  9. It Supports JsonPath and XmlPath which helps in parsing JSON and XML response. Rest Assured by default integrates both.
  10. 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

  1. To ensure that the implementation is working correctly as expected without any bugs!
  2. To ensure that the implementation is working as specified according to the requirements specification (which later on becomes our API documentation).
  3. 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:

  1. 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.
  2. Verify response payload: Verify the response body, field names, types and data, and also error responses.
  3. Verify response headers: Verify headers of each API response as this has an implication on both security and performance.
  4. 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:

  1. Positive tests – To check the basic functionality and acceptance criteria of API.
  2. Positive Tests with optional parameters – To include optional parameters and extra functionality in tests.
  3. Negative Tests with valid input – Test negative scenarios with valid inputs like trying to add existing username
  4. Negative Tests with invalid inputThis include testing the scenario like trying to add a username which is null
  5. Destructive testing – This a deeper form of negative testing where an intentional attempt to break the API is made to check its robustness