Skip to main content

Request and Response Spec Builders

Plan

Demonstrate the use of Spec Builders in REST Assured framework.

Program

Write a program to test the following REST API. Use spec builders to reduce the duplication of code.

  • Create Place API

Details are given below:

  • Base URI: https://rahulshettyacademy.com
  • Resource: /maps/api/place/add/json
  • HTTP Method: POST
  • Query Parameters: key=qaclick123
  • Content-Type: application/json

Request

{
"location": {
"lat": -38.383494,
"lng": 33.427362
},
"accuracy": 50,
"name": "Frontline house",
"phone_number": "(+91) 983 893 3937",
"address": "29, side layout, cohen 09",
"types": ["shoe park", "shop"],
"website": "http://google.com",
"language": "French-IN"
}

Response

{
"status": "OK",
"place_id": "928b51f64aed18713b0d164d9be8d67f",
"scope": "APP",
"reference": "736f3c9bec384af62a184a1936d42bb0736f3c9bec384af62a184a1936d42bb0",
"id": "736f3c9bec384af62a184a1936d42bb0"
}

Some things will be common across API's.

  • Ex:
    • Headers
    • Query Parameters
    • Base URL
    • Response Status Code and other assertions
    • Extracting the response

Re-Use common code across API's using spec builders

How to run the program ?

  • Clone the github repo
    • git clone git@github.com:smarigowda/RESTAssuredUdemyCourse.git
  • Open the project in Intellij IDE
  • Run the program SpecBuildersDemo

Notes

  • Request and Response spec builders use Builder Pattern to set various options and then create an object in the end. This is an excellent example of Builder design pattern.
  • Link to source - https://github.com/rest-assured/rest-assured/blob/master/rest-assured/src/main/java/io/restassured/builder/RequestSpecBuilder.java
  • Object returned by the spec builder is passed to .spec() method
  • Link to official docs - https://github.com/rest-assured/rest-assured/wiki/Usage#specification-re-use