What is the tech stack used for this?
The Java Cucumber Maven Test Automation Framework is a comprehensive solution for testing RESTful APIs. It makes use of the power of Java and Maven as a build tool, along with the simplicity of Cucumber, to provide a flexible and reliable testing framework. The framework has been designed to make test automation as straightforward and as hassle-free as possible.
Key Features
One of the key features of this framework is that it maintains the test context between steps, allowing for easy sharing of test data between steps. The framework uses REST-Assured for API testing, which provides a powerful and flexible API testing library. The framework also generates Cucumber reports for test results, providing a clear and concise view of the test results. The implementation of the BDD methodology provides a clear and understandable way to write tests, making it easy for stakeholders to understand the tests. In this framework, Lombok is utilized to reduce boilerplate code, such as getters and setters, and improve the overall maintainability of the code. With Lombok, you can annotate your class with just a few annotations, and it will generate the necessary code for you at compile time.
Finally, the framework allows for easy environment switching using dynamic config keys, making it easy to switch between different environments, such as DEV, QA, or UAT. This is achieved with the help of org.aeonbits.owner package.
package org.framework.bdd.utils;
import org.aeonbits.owner.Config;
@Config.LoadPolicy(Config.LoadType.MERGE)
@Config.Sources({"file:${user.dir}/src/main/resources/config.properties"})
public interface FrameworkConfiguration extends Config {
@Key("${environment}.base-uri")
String baseUri();
@Key("reports")
String reportPath();
}
environment = qa
reports = target/cucumber-reports/report.html
############## DEV ###################
dev.base-uri = https://api.restful-api.dev
############## QA #####################
qa.base-uri = https://api.restful-api.dev
Getting Started
Getting started with this framework is simple. You can clone the repository from https://github.com/pritesh-ranjan/java-cucumber-framework and open the project in IntelliJ IDEA or Eclipse. Then, run the command "mvn clean test" in the root directory of the project. The tests will automatically run, and the results will be generated in the target/cucumber-reports directory.
Framework Structure
The framework is structured into several key components:
src/main/java: contains abstract classes, test context, utilities, models, constants, and config reader
src/main/resources: contains the feature files and test data
src/test/java: contains the step definitions and test runners
pom.xml: contains the dependencies and build configuration
Test Context
Test context is an important aspect of any cucumber framework.
It is implemented using the Enum-based singleton pattern. The TestContext enum allows us to share test data in-between cucumber bdd steps in a thread-safe manner. In this case, we are using it to share payload, requests, and response objects. Credits to the test context : https://medium.com/@bcarunmail/sharing-state-between-cucumber-step-definitions-using-java-and-spring-972bc31117af
The test context class is located in the src/main/java directory.
API Testing
API testing is done using the REST-Assured library. The REST-Assured library provides a powerful and flexible API testing library that is used to make HTTP requests and verify the responses. We use the RequestSpecification class of rest assured for a one-time setup of the content headers, and other common items used while making a new request.
Behavior Driven Development
The framework uses Cucumber to implement BDD. The feature files are located in the src/test/resources directory and the step definitions are located in the src/test/java directory. BDD provides a clear and understandable way to write tests, making it easy for stakeholders to understand the tests.
Feature: Gadgets API tests
Background: reset the test context
Given test context is reset
Scenario Outline: Get object by id
Given a get request is made for fetching details for object with "<id>"
When response has status 200
Then response has valid schema
And response contains the following "<name>"
Examples:
| id | name |
| 2 | Apple iPhone 12 Mini, 256GB, Blue |
| 7 | HP Pavilion Plus |
This framework also demonstrates the use of Hooks like @Before and @After.
package org.framework.bdd.steps;
import io.cucumber.java.After;
import io.cucumber.java.Before;
public class Hooks {
@Before
public void beforeScenario() {
System.out.print("starting");
}
@After
public void afterScenario() {
System.out.print("ending");
}
}
Apart from hooks, the background steps are implemented with the Background keyword in the feature file.
While Background is a way to specify steps common to all scenarios in a feature file, hooks provide a way to run specific steps before or after each scenario or the entire feature. The Background steps are executed before each scenario in a feature file, whereas hooks are executed before or after each scenario or the entire feature, depending on where they are placed in the code. This allows you to perform specific actions, such as setting up test data or cleaning up after each scenario, in an efficient and organized manner. The use of hooks and Background can significantly improve the readability and maintainability of your test code.
Reporting
Cucumber reporting is integrated with the framework. After running the tests, the report can be found in the target/cucumber-reports directory. The report provides a clear and concise view of the test results. Currently, we are using cucumber HTML reports, but extent reporting can be added as per requirements.
Extensibility
The framework is built to be easily extensible and can be integrated with other testing libraries and frameworks. This makes it easy to add new functionality to the framework as needed, allowing for growth and expansion as your testing needs change. The AbstractSteps class store all common methods and variables. The FrameworkException class is a base exception class for all custom-defined exceptions.
Conclusion
The framework provides a powerful and flexible platform for test automation. With its combination of Java, Cucumber, and Maven, this framework provides a robust and maintainable solution for testing RESTful APIs using BDD methodology. Whether you are just getting started with test automation or looking for a more robust solution, this type of framework design is a great choice for your testing needs.
Top comments (0)