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. It 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.
Setup
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:
testCompile 'io.rest-assured:rest-assured:3.1.0'
Maven users, use this:
io.rest-assured rest-assured 3.1.0 test
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.
import io.restassured.RestAssured; import io.restassured.response.Response; import org.testng.Assert; public class RestTest { static String link = "https://api.spotify.com/v1/artists/1vCWHaC5f2uS3yhpwWbIA6/albums?album_type=SINGLE&offset=20&limit=1"; public static void main(String[] args) { Response response = RestAssured.get(link); response.then().assertThat().statusCode(200); //This fails response.then().assertThat().statusCode(401); //This passes } }
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.
// POST REQUEST import io.restassured.RestAssured; import io.restassured.response.Response; import io.restassured.specification.RequestSpecification; import org.testng.Assert; public class RestTest { static String json = "{place: 'earth', message: 'Hello'}"; static String link = "https://reqres.in/api/users"; public static void main(String[] args) { RequestSpecification spec = RestAssured.given(); spec.body(json); Response result = spec.post(link); result.then().assertThat().statusCode(201); // Success System.out.println(result.asString()); // Response body } }
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.
// DELETE REQUEST import io.restassured.RestAssured; import io.restassured.response.Response; import io.restassured.specification.RequestSpecification; import java.util.concurrent.TimeUnit; import org.testng.Assert; public class RestTest { static String link = "https://reqres.in/api/users/10"; public static void main(String[] args) { RequestSpecification spec = RestAssured.given(); Response done = spec.delete(link); done.then().assertThat().statusCode(204); System.out.println(done.getTimeIn(TimeUnit.NANOSECONDS)); } }
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.
Pingback: Multi-Cloud Implementation Strategies · Sweetcode.io