APIs, Be REST Assured



Building and maintaining an API is much like any application. Building it is probably the easiest part. But your work is not done when building is completed. You next need to test the API to ensure it functions as expected. What tools do you use for the test or series of tests? Even if you haven’t designed an API, you may need to test an API you’re considering using in a project. REST Assured is an API test framework that you will find extremely helpful in this situation.

REST Assured is a Java domain-specific language that simplifies the process of testing and validating REST services built on top of HTTP Builder. REST Assured supports validation of multiple request formats and has very intuitive syntax that makes it easy to learn.

In this age when companies are constantly delivering services over RESTful APIs, API testing is becoming increasingly important in development. API testing is challenging, but must be done right. There is little room for error in APIs. They are expected to be up all the time, to have a reasonable response time, and handle load efficiently. Imagine a Google Maps API malfunction. Many services we use daily would be affected, as their core functionality requires Google Maps. For Uber, Lyft, and Airbnb, Google Maps is critical.

In the examples that follow, we will explore REST Assured and find out exactly how to use it in API testing.


There are several ways to get REST Assured set up, which makes it easy to use the framework in any of your projects.

Gradle users need to add this line:

Maven users, use this:

Note that the method above includes both XmlPath and JsonPath. Check out GitHub to include only one of the two paths. Optionally, you can download REST Assured and the needed dependencies from here. In the examples that follow, we’ll work with only JSON, although Rest Assured supports XML as well.

Let’s Test Some APIs

With the setup complete, let’s create a Java test class for the Spotify API, without providing a token. This request fails with a 401 status code because the Spotify API expects a token. As expected, REST Assured will validate our query by comparing the expected outcome and the result from the API query using assertion.

There are a couple of things to notice here. First is the syntax. It is simple to understand, as it is closer to natural speech. Other syntactic sugar you may encounter includes given, and, then, and expect.

At this point, we will perform POST and DELETE requests using ReqRes. ReqRes simulates an API with fake data, yet real responses. Therefore, we don’t have to build one from scratch. POST will enable us to create new resources, and DELETE, as the name suggests, will delete a resource.

You have successfully created some data on the server. The assertion ensures that the data was indeed written successfully. Note that the corresponding status code for a successful POST request is 201, not 200.

This request fails as we cannot delete the data on the server; hence, the 204 (no content). However, we’ve queried the time, in nanoseconds, that it took to make the request. This can come in handy when you want to know how fast an API performs.

These examples are just a few things you can do with REST Assured. More complex automated tests can be created for APIs with other formats as well—not only JSON. Some good places to begin are the GitHub wiki and javadocs for more advanced features.

Have a good time testing and validating APIs.

Do you think you can beat this Sweet post?

If so, you may have what it takes to become a Sweetcode contributor... Learn More.

Bruno is a junior at Ashesi University College studying Computer Science. He is interested in leveraging the power of technology to increase productivity. As a big fan of open source technology, he is currently exploring the possibility of using the Bitcoin Blockchain to fight corruption in government.


Click on a tab to select how you'd like to leave your comment

Leave a Comment

Your email address will not be published. Required fields are marked *