As a part of End to End REST Assured Tutorial , in this post We will write first GET request in REST assured.
I will strongly recommended you to learn basics of API testing from here.
We will automate a GET request named “GetBookingIds” from Restful Booker. We will verify status code and line of request.
I will not start with BDD pattern or builder pattern as of now. I will go step by step so that you do not confuse with the flow.
Let’s start with some basic points about Rest Assured:-
- 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 of 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. We will see hierarchy of classes and interfaces later. - 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.
REST Assured code example:-
Non-BDD Style Code:-
package RestfulBooker.getExamples; 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 Code:-
package RestfulBooker.getExamples; import org.hamcrest.Matchers; import org.testng.annotations.Test; import io.restassured.RestAssured; public class BDDStyleGetRequest { @Test public void GetBookingIds_VerifyStatusCode() { // Given RestAssured.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.sum()", Matchers.hasSize(10)) // To verify booking id at index 3 .body("bookingid[3]", Matchers.equalTo(1)); } }
BDD with static import style code:-
package RestfulBooker.getExamples; import static org.hamcrest.Matchers.*; import static io.restassured.RestAssured.*; import org.testng.annotations.Test; public class BDDStyleGetRequestWithStaticImport { @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)); } }
You can clone/download example repo here.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning
You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.
in the 3rd point “”Response interface extends
Validatable Interface”” it s correct statement ?
Nice Tutorials, very well explained
Thanks.. this is the best part of your tutorials as you target on the core concepts
just to let others aware belwo class needs to be imported as well
import static org.hamcrest.CoreMatchers.equalTo;
and assertion can fail as data keeps on changing for the APIs
200 should be successful though
good create a youtue videos
So this post makes me realize the importance of static import as here we can avoid the same name again and again.
So in such cases static import is good to have
java.lang.SecurityException: Signers of ‘org.hamcrest.Matchers’ do not match signers of other classes in package
getting this error, cn you please help?
Put hamcrest jara before junit in build path