Automating Your API Tests With REST Assured

Now that global developers are using APIs for software and app development services, the use of these APIs is increasing exponentially. Following the increasing trend of the APIs in software development, proper automated testing of these APIs is becoming indispensable.

To make things easier, there are many tools available to assist developers in writing these automated tests at the API level. However, I’d like to put some light on one of the best ones available – REST Assured.

Rest Assured is an Open Source tool and the most popular Java library, which is commonly used in API Test Automation in most of the companies. It provides developers a domain-specific language to write powerful, maintainable tests for RESTful APIs. Here is the complete detail on automating your API tests with Rest Assured.

What is API?

API stands for Application Programming Interface that comprises a set of functions that can be executed and accessed by another software system. In simple words, an API serves as an interface between different software systems in order to help establish their interaction and data exchange.

What is REST Assured?

Rest Assured is one of the leading and most renowned Open Source Java-based DSL that offers a great advantage to the developers as this supports multiple HTTP requests. REST Assured is capable of validating and/or verifying the responses of these requests. The requests that REST Assured can handle include DELETE, PUT, GET, POST, OPTIONS, PATCH, and HEAD methods.

Apart from supporting various requests, REST Assured also allows them to be constructed with multiple parameters.

Getting Started with REST Assured

It’s very easy and quick to get started with REST Assured as you simply need to add this as a dependency to your project. You can use the following entry to your pom.xml if using Maven:

REST Assured is a flexible tool that you can use easily in combination with existing unit testing frameworks, such as JUnit and TestNG. Once you are done with the initial configuration of REST Assured, add the following static imports to your test class:

And you’re all set to create your first REST Assured test.

Validating technical response data

REST Assured is an advanced tool that not just lets you verify the response body contents, it also allows you to check the correctness of technical response data so you know that everything is working as per your expectation. For example, you can check the HTTP response status code, the response content type, etc.

Followings are some examples:

The response status code is equal to 200

The response content type equals “application/JSON.”

The value of the response header “Content-Length” equals “4567.”

Parameterizing tests

There may be many times when you’d need to repeat the same tests using various sets of parameters to be double sure about its performance. However, you will never want to write a new test for each test data record. REST Assured allows you to create a parameterized test and feed it with as many test data records. Followings are the different parameters that RESTful APIs support:

Query parameters

Path parameters

Accessing secured APIs

Software APIs are usually secured with authentication mechanisms. With REST Assured, it’s quite easy as this supports basic, digest, form, and OAuth authentication. Here is the example that you can check to know how to call a RESTful API that has been secured using basic authentication:

REST Assured help make automating your APIs testing an easier process. Integrate it to your working environment to ensure better performance with assured peace of mind.

About The Author

Vipul has over 3+ years of industry experience in the area of Software Testing (Manual and Automation) with a solid understanding of Test Planning, Test Design, Test Execution and Defect Reporting & Tracking. He meet with system users to understand the scope of projects and works with software developers and project support team.