We are now ready to create our first Rest-Assured API test so we will begin by making a request to the Web API restful-booker specifically the ping endpoint.
Start by creating a new Test in your class and add the following code into the Test:
Response response = given().get("https://restful-booker.herokuapp.com/ping/");
given() method is where Rest-Assured starts. The method itself doesn’t do anything but allows you to use a design pattern known as method chaining (I.E. having a method return a different method that can be called in the same expression) to call the method
get() method takes one parameter, the URI of the Web API we want to send a request to. In this case it’s the health check endpoint of the Auth API. We are then saving the results of the API request in a Rest-Assured Response object for future use.
At the moment our test doesn’t have anything to assert on, so let’s create a simple assertion based on the status code of the response we get back.
Extend the test to assert for a status code of 200:
Response response = given().get("https://restful-booker.herokuapp.com/ping/"); int statusCode = response.getStatusCode(); assertThat(statusCode, is(201));
The response we get back from the Web API is stored in the
response object, which we can then call additional methods on to get specific details from the Response. In this case we are calling
getStatusCode() to retrieve the HTTP response status code and then asserting it equals 200.