- RestAssured is a class which consists many static fields and methods.
It supports POST, GET, PUT, DELETE, HEAD, PATCH and OPTIONS requests and to verify the response of these requests. - RestAssured has a static overloaded method named get() which returns a reference to the Response interface. In fact return type of all http methods in RestAssured class is of type Response. This response contains every details returned by hitting request i.e. response body, response headers, status code, status lines, cookies etc.
- To validate response like status code or value , we need to get reference of type ValidatableResponse. ValidatableResponse is an interface. Response interface has a method named “then()” which returns ValidatableResponse. In fact there is an interface called “Validatable” which has “then()” method. Response interface extends Validatable Interface. The implemented class of Response interface is RestAssuredResponseImpl.
- Once we get ValidatableResponse reference, we can do many assertions. In this post, we will verify status code and status line. It consists of many validation methods.
eg:-
Non-BDD Style Code:
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;
import io.restassured.specification.RequestSpecification;
public class NonBDDStyleGetRequest {
// Without static import and builder pattern
@Test
public void GetBookingIds_VerifyStatusCode() {
// Create a request specification
RequestSpecification request= RestAssured.given();
//Adding URI
request.baseUri("https://restful-booker.herokuapp.com/booking");
// Calling GET method on URI. After hitting we get Response
Response response = request.get();
// Let's print response body.
String resString = response.asString();
System.out.println("Respnse Details : " + resString);
/*
* To perform validation on response like status code or value, we need to get
* ValidatableResponse type of response using then() method of Response
* interface. ValidatableResponse is also an interface.
*/
ValidatableResponse valRes = response.then();
// It will check if status code is 200
valRes.statusCode(200);
// It will check if status line is as expected
valRes.statusLine("HTTP/1.1 200 OK");
}
}
BDD Style In Rest Assured
Rest Assured allows you to arrange your test script in BDD style using Given, When and Then. Hitting an API has three steps:-
Prerequisites to perform Request:- GIVEN
E.g.- Setting the Base URI, Base Path, Content Type , Request body (Payload) , cookies, headers , authentication , params etc.
Performing Request :- WHEN
E.g:- Which HTTP request to hit i.e. HTTP verbs – GET, POST etc
Verification and extracting response post hitting :- THEN
E.g. Verify status code, response data, log, extracting data etc.
import static io.restassured.RestAssured.*;
import static io.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;
import org.testng.annotations.Test;
public class GetBookingIds_RestfulBookerUsingBDDStyleAndStaticImport {
@Test
public void GetBookingIds_VerifyStatusCode() {
// Given
given()
.baseUri("https://restful-booker.herokuapp.com")
// When
.when()
.get("/booking")
// Then
.then()
.statusCode(200)
.statusLine("HTTP/1.1 200 OK")
// To verify booking count
.body("bookingid", hasSize(10))
// To verify booking id at index 3
.body("bookingid[3]", equalTo(1));
}