REST API Automation Test project using Rest Assured and Cucumber BDD .
- JDK 1.8
- Maven - build tool
- Junit - test runner
- Cucumber - BDD/Gherkin style feature files and Step Definitions
- Rest assured - Rest api verification library
This is maven project , so start with
mvn clean install
BDD requires a feature file to invoke the step definitions:
- Create the scenarios in feature file as per the requirements, so each step in feature file has to match a step definition in class file;
- Following the BDD practices for coding;
- Using the special annotation like "@Before" which is the first method to run for each scenario. Moreover, this is the right place to set up the URI (endpoint) which will be called by HTTP request;
###REST API
- This project is aimed at calling Jsonplaceholder' API to validate the Posts. This is written in a feature file using Cucumber.
- Each line of the scenario is tied to backend code that actually executes the line (step).
Testing a simple response containing some JSON data.
- Request URL: https://jsonplaceholder.typicode.com/posts/1
- Request Method: GET
- Response Content-Type: application/json
- Response Body:
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}
- Status Code: 200 OK
- Request URL: https://jsonplaceholder.typicode.com/posts/101
- Request Method: GET
- Response Body:
{
}
- Status Code: 404 Not Found
- The main goal is to ensure that the JSON format is correct as well as all data inside it. Therefore, it was created a json file (schema) and stored it in "resource" file in the package project. In the source-code is validated by the statement 'body(matchesJsonSchemaInClasspath(" file path json"))';
- when().get("/" +Id)
- then().log().all();
- then().statusCode(Integer.parseInt(statusCode));
- then().assertThat().contentType(ContentType.JSON).and().body(matchesJsonSchemaInClasspath("schema-json.json"));