Now that we can use Rest-Assured to send an HTTP request, let’s look at how we can use Rest-Assured to access an HTTP response body.
For this lesson we will continue to use the https://restful-booker.herokuapp.com/booking/1. Let’s take a look at the JSON body from the HTTP response:
{
"firstname": "Susan",
"lastname": "Ericsson",
"totalprice": 378,
"depositpaid": true,
"bookingdates": {
"checkin": "2018-11-23",
"checkout": "2018-12-03"
},
"additionalneeds": "Breakfast"
}
We’re going to assert that the booking has Breakfast set as an additional need, so start by creating a new test adding the following code:
Response response = given().get("https://restful-booker.herokuapp.com/booking/1");
String responseBody = response.getBody().print();
assertThat(responseBody, containsString("Breakfast"));
The test starts very similar to the previous test we created, using Rest-Assured to send a GET request and storing that in a Response object. We then call getBody()
to get the HTTP response body and then print()
to convert it to a string.
Finally we are asserting that the response body contains a sub string of Breakfast
which isn’t a great way to assert the roomid. Partially because the assertion is brittle for example it won’t handle data type changes well or if the value appears in other areas of the body. Plus what if we wanted to extract the value to use in a future HTTP request?
When an HTTP request or response is sent it is sent as plain text. This means that when we receive the HTTP response in Rest-Assured, the response body is a String. However, we can use the technique of De-serialising to convert it from a String into a Java object that we can then use to extract specific values. It may sound a little odd, but follow the guide and you will see how it works:
First off, to get de-serialisation working we will need to add the following dependencies to our pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
To make deserialisation work, we need to create a series of classes that match the structure of the JSON we want to deserialise. These types of classes are known as POJOs or Plain Old Java Objects.
We start by creating two new classes one called BookingResponse
and one called BookingDates
and add variables into the classes that match the name and type of each entry in the JSON we want to deserialise. For example firstname would be private String firstname
:
@JsonProperty
private String firstname;
@JsonProperty
private String lastname;
@JsonProperty
private int totalprice;
@JsonProperty
private boolean depositpaid;
@JsonProperty
private BookingDates bookingdates;
@JsonProperty
private String additionalneeds;
As you can see the variable names match the names of the keys in the JSON body response and the data types match the data types of the values in the JSON object. Also, notice how bookingdates
refers to the BookingDates
class we created. This is because the bookingdates
section in the body response is a sub object, so we have to create a class to match. So open up BookingDates
and copy in the following:
@JsonProperty
private Date checkin;
@JsonProperty
private Date checkout;
Now we have the JSON body from the HTTP response mapped out in Java classes we can then add in methods to extract the data we need. So in the BookingResponse
class add the following method in:
public String getAdditionalneeds() {
return additionalneeds;
}
With the Java classes all setup, we can now update the test to the following and run it:
Response response = given().get("https://restful-booker.herokuapp.com/booking/1");
BookingResponse responseBody = response.as(BookingResponse.class);
String additionalneeds = responseBody.getAdditionalneeds();
assertThat(additionalneeds, is("Breakfast"));
Let’s break down what happened. The first line is where we send the HTTP request to the Web API and get a response back, which we store as Rest-Assured Response
object. On the second line we take the response and call the method as()
and provide it with a reference to our BookingResponse
class by adding in BookingResponse.class
as a parameter. This tells Rest-Assured to map the HTTP response body to our Java classes. If the class didn’t match with the response body then you would have received an error. Finally, we extract the string value using getAdditionalNeeds()
and then assert that it equals Breakfast.
Comments