Step-by-Step Guide to API Automation Framework with Rest-Assured, Java, and Cucumber
Keshab Fouzdar
Full Stack QA Engineer | ISTQB? Certified | AWS Certified Cloud Practitioner | Experian PowerCurve Collection Developer | Banking Domain | Selenium, RestAssured, Playwright, Codecept.js, Cypress
Introduction
Rest-Assured is a popular Java library for testing and validating RESTful APIs, known for its simplicity and powerful features. Cucumber, on the other hand, is a Behavior-Driven Development (BDD) tool that allows writing test cases in plain English, making them accessible to non-technical stakeholders.
By combining these tools, this guide aims to help you build a robust and maintainable API automation framework. You will learn how to:
Whether you are a beginner or an experienced automation engineer, this guide will equip you with the knowledge and tools necessary to automate API testing effectively, ensuring higher quality and faster delivery of your software products.
Prerequisites
Step 1: Set Up Your Project
xml
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>api-automation</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Rest-Assured -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.3.3</version>
<scope>test</scope>
</dependency>
<!-- Cucumber -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>6.10.4</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>6.10.4</version>
<scope>test</scope>
</dependency>
<!-- Gson for JSON parsing -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<includes>
<include>**/*Test*.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Step 2: Project Structure
Create the following directory structure in your project:
src
├── main
│ └── java
│ └── com
│ └── example
│ └── api
│ ├── utils
│ ├── models
│ └── steps
└── test
└── java
└── com
└── example
└── api
├── runner
└── features
Step 3: Create Feature Files
gherkin
Feature: API Testing with Rest-Assured and Cucumber
Scenario: Get user details
Given I set the base URI to "https://reqres.in"
When I send a GET request to "/api/users/2"
Then the response status code should be 200
And the response should contain the user's details
Step 4: Implement Step Definitions
java
package com.example.api.steps;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import io.rest-assured.RestAssured;
import io.rest-assured.response.Response;
import static org.hamcrest.Matchers.equalTo;
public class ApiSteps {
private Response response;
@Given("I set the base URI to {string}")
public void setBaseUri(String baseUri) {
RestAssured.baseURI = baseUri;
}
@When("I send a GET request to {string}")
public void sendGetRequest(String endpoint) {
response = RestAssured.get(endpoint);
}
@Then("the response status code should be {int}")
public void verifyStatusCode(int statusCode) {
response.then().statusCode(statusCode);
}
@Then("the response should contain the user's details")
public void verifyUserDetails() {
response.then().body("data.id", equalTo(2));
response.then().body("data.email", equalTo("[email protected]"));
response.then().body("data.first_name", equalTo("Janet"));
response.then().body("data.last_name", equalTo("Weaver"));
}
}
领英推荐
Step 5: Create a Test Runner
java
package com.example.api.runner;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions(
features = "src/test/java/com/example/api/features",
glue = {"com.example.api.steps"},
plugin = {"pretty", "html:target/cucumber-reports.html"},
monochrome = true
)
public class TestRunner {
}
Step 6: Running the Tests
To execute your tests, run the TestRunner class. This will trigger the Cucumber test run, which will read the feature files, match the steps to the definitions, and execute the tests using Rest-Assured.
Framework Details
Directory Structure:
Dependency Management:
Test Execution:
Assertions:
Reporting:
Conclusion
This guide provides a comprehensive framework setup for API automation using Rest-Assured, Java, and Cucumber. By following these steps, you can create a robust and maintainable test automation framework that leverages the power of Cucumber for BDD and Rest-Assured for API testing.
Feel free to expand this framework by adding more utility functions, handling complex JSON structures, and integrating with CI/CD pipelines for continuous testing. Happy testing!