This project is a flexible API automation testing boilerplate combining TestNG (for traditional/hybrid testing) and Cucumber Java (for BDD-style testing).
- Java SDK: Version 22
- Test Runner: TestNG (7.8.0)
- BDD Framework: Cucumber Java (7.14.0)
- HTTP Client: RestAssured (5.3.1)
- Reporting: Allure Report (2.25.0)
- Build Tool: Maven
api-testng-example/
├── xml/ # TestNG Suite Configurations
│ ├── testng.xml # Runs all tests (Hybrid + BDD)
│ └── testng-cucumber.xml # Runs BDD Cucumber tests only
├── src/
│ ├── main/
│ │ └── java/ # Main Logic & API Configurations
│ │ ├── connector/
│ │ │ └── Connector.java # RESTAssured API Client / API Call Triggers
│ │ └── data/
│ │ ├── Endpoint.java # Base URL & Endpoint Paths
│ │ └── Payload.json # JSON payload data file
│ └── test/
│ └── java/ # Test Scenarios
│ ├── testng/ # Traditional TestNG Testing
│ │ ├── TestGetRequest.java
│ │ └── TestPostRequest.java
│ └── cucumber/ # Cucumber BDD Testing
│ ├── features/
│ │ └── user_api.feature # Gherkin Scenario (.feature)
│ ├── steps/
│ │ └── UserSteps.java # Gherkin steps implementation code
│ └── runner/
│ └── CucumberRunner.java # TestNG Runner for Cucumber
└── pom.xml # Maven Project Model (Dependencies & Java Version)
- Open Endpoint.java.
- Customize the
BASE_URLand endpoint path constants:public class Endpoint { public static final String BASE_URL = "https://reqres.in/api"; public static final String USERS = "/users"; }
- Open Connector.java.
- Add a new method using RestAssured syntax (GET, POST, PUT, DELETE, etc.):
public Response putRequest(int userId, String payload) { String path = Endpoint.USERS + "/" + userId; return given() .header("Content-Type", "application/json") .body(payload) .when() .put(baseUrl + path) .then() .extract().response(); }
- Create a new Java class under src/test/java/testng.
- Write a method annotated with TestNG's
@Test, instantiateConnector, and write your assertions. - Register the new test class under the
<classes>tag in xml/testng.xml.
- Create a new Gherkin scenario using
Given,When,Then,Andkeywords inside src/test/java/cucumber/features (with a.featureextension). - Implement the Gherkin steps in Java under src/test/java/cucumber/steps (either add them to
UserSteps.javaor create a new step class). - The scenario will execute via xml/testng-cucumber.xml which calls CucumberRunner.java by default.
This boilerplate is designed to be easily pruned if you prefer to focus on a single testing style:
- Directories to remove:
- Delete the entire src/test/java/cucumber folder.
- Delete the suite configuration file xml/testng-cucumber.xml.
- Configuration Cleanup:
- In xml/testng.xml, remove the
<test name="Cucumber BDD Scenario">block that callscucumber.runner.CucumberRunner. - (Optional) Remove
cucumber-java,cucumber-testng, andallure-cucumber7-jvmdependencies frompom.xml.
- In xml/testng.xml, remove the
- Directories to remove:
- Delete the entire src/test/java/testng folder.
- Configuration Cleanup:
- In xml/testng.xml, remove the
<test name="GET Scenario">and<test name="POST Scenario">blocks so the suite only runs thecucumber.runner.CucumberRunner.
- In xml/testng.xml, remove the
Ensure your local environment uses JDK 22.
# Run all tests (Traditional TestNG & Cucumber BDD)
mvn clean test
# Run BDD Cucumber scenarios only
mvn test -DsuiteXmlFile=xml/testng-cucumber.xml
# Run Traditional TestNG tests only (using the main suite config)
mvn test -DsuiteXmlFile=xml/testng.xmlAllure results will be saved in the target/allure-results folder. To view the report locally in your browser, run:
mvn allure:serve