top of page
vijetha.asam

Rest Assured with Cucumber BDD API Automation

What is API:  Application Programming Interface, it is an interface and also called  a middle tier layer. API is an interface (or) communication protocol between a client and server intended to simplify the building of client-side software.

API Automation: Application Programming Interface (API) automation testing is a type of automated testing that focuses on the performance and functionality of API’s.


API testing is the process of sending requests to an API and monitoring the responses to ensure its behaving as expected.


Rest Assured is a Java-based library that is used to test RESTful Web services/API’s. In the Java programming language we are using the Rest-Assured library for API Automation.


Rest-Assured is a Java jar, which we use in our project to do all the validations. If we use Python, Python has another library called Request library.


RESTful API Testing ( or) REST API is an open-source automation technique testers use for JSON and XML-based web applications.


Cucumber is an open-source testing framework that supports Behavior Driven Development for automation testing. The tests are first written in a simple scenario form that describes the expected behavior of the system from the user’s perspective.


BDD (Behavior Driven Development) testing is an Agile approach to software testing where testers write test cases in simple language that even people without technical expertise can understand. The goal of BDD testing is to increase collaboration between the technical side and the business side of the organization.


The Gherkin language is a unique business-readable language used to describe system behaviors and scenarios. It is structured by three primary statements: Given, When, and Then, with each statement describing a specific aspect of the system.

  • Given statement sets up the initial context for the behavior and defines the starting point of the system. 

  • When statement  describes the trigger that brings about a change or behavior in the system. 

  • Then statement  defines the expected outcome that should be observed after the event mentioned in the When statement.


Prerequisites for creating the project :

  • Java 9+ and Eclipse

  • Maven project

  • Install Cucumber plugin

Create the maven project and add  dependencies  in pom.xml:

  • Rest assured

  • Cucumber java

  • Cucumber junit(for running cucumber test)

  • Jackson databind (for converting json to java object (or) java object to json )


Feature file with post request:

Cucumber BDD mainly consists of three major parts - feature file, step definitions and test runner.

Feature file contains scenarios, we can simply create feature file with .feature extension. In cucumber TestCases are represented as Scenarios.



StepDefinitions:

A step definition file stores the mapping data between each step of the scenario defined in the feature file and the code to be executed.

Implement Steps for this scenarios to executing the feature file



Create one class for step definitions:



To parse the json (or) to construct the json as a request body,here we are implementing Serialization and Deserialization of Request/Response with POJO classes.


Serialization: Rest Assured context is a process of converting a java object into Request body (or) Request payload with the help of POJO classes.

 

Java objects are constructed with the support of POJO classes. POJO classes are created based on the request/response payload.


Deserialization: Rest Assured also supports deserialization by converting Response body back to Java Object. With the help of this it is easy to parse and extract response json values if they are wrapped as java objects.


Here are the payload classes of our json: This  json has Nested Objects.

Parent payload:



package payload;

import java.util.List;

public class GooglePOJO {


private LocationPOJO location;

private int accuracy;

private String name;

private String phone_number;

private String address;

private List<String> types;

private String website;

private String language;


public LocationPOJO getLocation() {

return location;

}

public void setLocation(LocationPOJO location) {

this.location = location;

}

public int getAccuracy() {

return accuracy;

}

public void setAccuracy(int accuracy) {

this.accuracy = accuracy;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPhone_number() {

return phone_number;

}

public void setPhone_number(String phone_number) {

this.phone_number = phone_number;

}

public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

public List<String> getTypes() {

return types;

}

public void setTypes(List<String> types) {

this.types = types;

}

public String getWebsite() {

return website;

}

public void setWebsite(String website) {

this.website = website;

}

public String getLanguage() {

return language;

}

public void setLanguage(String language) {

this.language = language;

}


}



This is Child payload:



package payload;

public class LocationPOJO {


private double lat;

private double lng;


public double getLat() {

return lat;

}

public void setLat(double lat) {

this.lat = lat;

}

public double getLng() {

return lng;

}

public void setLng(double lng) {

this.lng = lng;

}

}



ReUsable Methods: 


Create  test data in a separate class, that class data can be used in project for other requests.


package resources;

import java.util.ArrayList;

import java.util.List;

import payload.GooglePOJO;

import payload.LocationPOJO;

public class TestDataOfAddPlace {

public GooglePOJO reUsableData() {

GooglePOJO gp=new GooglePOJO();

gp.setAccuracy(value);

gp.setName("value1");

gp.setLanguage("value2");

gp.setAddress("value3");

gp.setPhone_number("value4");

gp.setWebsite("value5");


LocationPOJO lp=new LocationPOJO();

lp.setLat(value6);

lp.setLng(value7);

gp.setLocation(lp);


List<String> list=new ArrayList<String>();

list.add("value8");

list.add("value9");

gp.setTypes(list);

return gp;

}

}



Request Specification and Response Specification:

The RequestSpecBuilder class contains methods that can help set cookies, headers, authentication, and other elements. The RequestSpecification interface can be used to extract repetitive actions, such as setting headers and base URLs. The ResponseSpecification interface can be used when similar assertions need to be done for multiple Rest requests.



package resources;

import io.restassured.RestAssured;

import io.restassured.builder.RequestSpecBuilder;

import io.restassured.http.ContentType;

import io.restassured.specification.RequestSpecification;

public class ReUsableRecSpec {


public RequestSpecification recSpec() {

RequestSpecification requestSpec;


RestAssured.baseURI="URL";



requestSpec=new RequestSpecBuilder().setBaseUri("URL").addQueryParam("key","KEY")

.setContentType(ContentType.JSON).build();

return requestSpec;

}

}



Implementing Steps in step definition file with given(),when() and then().

given() method to set up the request(all input details).

 when() method to specify the HTTP method and URL(submit the API).

 then() method to validate the response.



package stepDefinitions;

import static io.restassured.RestAssured.given;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;

import java.util.List;

import io.cucumber.java.en.Given;

import io.cucumber.java.en.Then;

import io.cucumber.java.en.When;

import io.restassured.RestAssured;

import io.restassured.builder.RequestSpecBuilder;

import io.restassured.builder.ResponseSpecBuilder;

import io.restassured.http.ContentType;

import io.restassured.response.Response;

import io.restassured.specification.RequestSpecification;

import io.restassured.specification.ResponseSpecification;

import payload.GooglePOJO;

import payload.LocationPOJO;

import resources.ReUsableRecSpec;

import resources.TestDataOfAddPlace;

public class PostRequest extends ReUsableRecSpec{


RequestSpecification res;

ResponseSpecification responseSpec;

Response response;


TestDataOfAddPlace data=new TestDataOfAddPlace();

@Given("User Creates the Post Request for Add place")

public void user_creates_the_post_request_for_add_place() {


 res=given().log().all().spec(recSpec()).body(data.reUsableData());

}

@When("User sends http post request with valid endpoints")

public void user_sends_http_post_request_with_valid_endpoints() {


 responseSpec=new ResponseSpecBuilder().expectStatusCode(200)

.expectContentType(ContentType.JSON).build();

 response=res.when().post("endpoints")

.then().log().all().spec(responseSpec)

.extract().response();

}

@Then("User receives the status code {int} with response body")

public void user_receives_the_status_code_with_response_body(Integer int1) {

       assertEquals(response.getStatusCode(),200);

}

}




TestRunner:

The test runner file executes the feature files and coordinates the steps in those files with the corresponding step definitions.


package cucumber.runner;

import org.junit.runner.RunWith;

import io.cucumber.junit.Cucumber;

import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)

@CucumberOptions(features="src/test/resources/features",glue= {"stepDefinitions"})

public class TestRunner {

}



Response of the Post Request:


Keep learning,

Thank you.








343 views

Recent Posts

See All
bottom of page