Using ResponseSpecBuilder of Rest Assured ( Code Reuse )
In the previous post, we learned about Using RequestSpecBuilder of Rest Assured. In this post, we will learn about using ResponseSpecBuilder of Rest Assured. You can use the builder to construct a response specification. ResponseSpecBuilder is usually used when we have some common things to check across multiple tests. For e.g. the status code returned once the API is hit is 200, the contentType of the response returned is JSON, etc.
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import io.restassured.RestAssured; import io.restassured.http.ContentType; import static io.restassured.RestAssured.*; public class GetStatusCodeTest { @BeforeClass public void requestSpec () { RestAssured.baseURI="https://maps.googleapis.com"; } @Test public void Test01() { given() .param ("query", "restaurants in mumbai") .param ("key","Xyz" ) .when () .get ("/maps/api/place/textsearch/json") .then () .contentType (ContentType.JSON) .statusCode (200); } }
In above example, the parameters to be sent i.e query and key are mentioned in test021 as well as the base path "/maps/api/place/textsearch/json" is mentioned in test02. Now we use ‘then‘ part to check the response. We simply add a check ‘contentType(ContentType.JSON)‘ to make sure that the response we get is in JSON format.Under assertThat method, we add a check that the statusCode (200) is returned in response.
It would obviously happen that we have multiple tests in which we have check whether the response returned is in JSON and the Status Code is 200. In order to avoid this, we will make use of ResponseSpecBuilder of Rest Assured.
Now we will work with the same example using ResponseSpecBuilder.
import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import static io.restassured.RestAssured.*; import io.restassured.RestAssured; import io.restassured.builder.ResponseSpecBuilder; import io.restassured.http.ContentType; import io.restassured.specification.ResponseSpecification; public class ResponseSpecificationTest { ResponseSpecBuilder builder; ResponseSpecification rspec; @BeforeClass public void requestSpec () { RestAssured.baseURI="https://maps.googleapis.com"; ResponseSpecBuilder builder = new ResponseSpecBuilder (); builder.expectContentType (ContentType.JSON); builder.expectStatusCode (200); rspec= builder.build (); } @Test public void Test01() { given() .param ("query", "restaurants in mumbai") .param ("key","xyz" ) .when () .get ("/maps/api/place/textsearch/json") .then () .spec (rspec); } }
For responseSpec method, we specify a BeforeClass annotation of TestNG so that it would run before the tests mentioned in the class. In responseSpec method, we create an object of ResponseSpecBuilder.
Once the object is created then we mention expected content type and expected status code.
In test01 method, Under Given, we will mention the parameters while making the API call.When is used for making the API call. Under Then we just use ResponseSpecification rspec, instead of specifying the checks for Content Type and Status. Now the same spec can be used across multiple tests.
In the next post, we will learn about Logging in Rest Assured.
In the next post, we will learn about Logging in Rest Assured.
Comments
Post a Comment