Skip to content

lynix28/api-testng-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Boilerplate API Automation Testing (TestNG & Cucumber Java)

This project is a flexible API automation testing boilerplate combining TestNG (for traditional/hybrid testing) and Cucumber Java (for BDD-style testing).


🛠️ Stack & Specifications

  • 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

📂 Project Directory Structure

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)

⚙️ Development & Usage Guide

1. Modifying Endpoints & Path URLs

  • Open Endpoint.java.
  • Customize the BASE_URL and endpoint path constants:
    public class Endpoint {
        public static final String BASE_URL = "https://reqres.in/api";
        public static final String USERS = "/users";
    }

2. Adding a New API Call

  • 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();
    }

3. Adding Test Scenarios

A. Traditional TestNG Style (Non-BDD)

  1. Create a new Java class under src/test/java/testng.
  2. Write a method annotated with TestNG's @Test, instantiate Connector, and write your assertions.
  3. Register the new test class under the <classes> tag in xml/testng.xml.

B. Cucumber BDD Style

  1. Create a new Gherkin scenario using Given, When, Then, And keywords inside src/test/java/cucumber/features (with a .feature extension).
  2. Implement the Gherkin steps in Java under src/test/java/cucumber/steps (either add them to UserSteps.java or create a new step class).
  3. The scenario will execute via xml/testng-cucumber.xml which calls CucumberRunner.java by default.

🧹 Framework Modularity (Excluding Unused Folders)

This boilerplate is designed to be easily pruned if you prefer to focus on a single testing style:

Using Traditional TestNG Only (No Cucumber BDD)

  • Directories to remove:
  • Configuration Cleanup:
    • In xml/testng.xml, remove the <test name="Cucumber BDD Scenario"> block that calls cucumber.runner.CucumberRunner.
    • (Optional) Remove cucumber-java, cucumber-testng, and allure-cucumber7-jvm dependencies from pom.xml.

Using Cucumber BDD Only (No Traditional TestNG)

  • Directories to remove:
  • Configuration Cleanup:
    • In xml/testng.xml, remove the <test name="GET Scenario"> and <test name="POST Scenario"> blocks so the suite only runs the cucumber.runner.CucumberRunner.

🚀 Running the Tests

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.xml

📊 Allure Reporting

Allure results will be saved in the target/allure-results folder. To view the report locally in your browser, run:

mvn allure:serve

About

API automation test project example using TestNG

Topics

Resources

Stars

Watchers

Forks

Contributors