Cucumber with Java, TestNG and Reports (Overview)
Cucumber in Java
Cucumber is the original BDD tool. The Gherkin feature files look exactly the same as in Behave. Only the step definitions are written in Java.
Dependencies (pom.xml)
xml
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.x.x</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>7.x.x</version>
</dependency>
Folder Layout
text
src/test/java/
stepdefs/LoginSteps.java
hooks/Hooks.java
runners/TestRunner.java
base/DriverFactory.java
pages/LoginPage.java
src/test/resources/
features/login.feature
Step Definitions
java
package stepdefs;
import base.DriverFactory;
import io.cucumber.java.en.*;
import org.testng.Assert;
import pages.*;
public class LoginSteps {
private LoginPage loginPage;
private InventoryPage inventory;
@Given("I am on the login page")
public void iAmOnTheLoginPage() {
loginPage = new LoginPage(DriverFactory.get()).open();
}
@When("I log in with {string} and {string}")
public void iLogInWith(String user, String password) {
inventory = loginPage.loginAs(user, password);
}
@Then("I should see the products page")
public void iShouldSeeTheProductsPage() {
Assert.assertEquals(inventory.title(), "Products");
}
@Then("I should see the error {string}")
public void iShouldSeeTheError(String message) {
Assert.assertTrue(loginPage.errorMessage().contains(message));
}
}
{string} matches text in double quotes. Other Cucumber expressions are {int}, {float} and {word}.
Hooks
java
package hooks;
import base.DriverFactory;
import io.cucumber.java.*;
import org.openqa.selenium.*;
public class Hooks {
@Before
public void setUp() {
DriverFactory.create("chrome", false);
}
@After
public void tearDown(Scenario scenario) {
WebDriver driver = DriverFactory.get();
if (scenario.isFailed() && driver != null) {
byte[] png = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
scenario.attach(png, "image/png", "failure");
}
DriverFactory.quit();
}
}
TestNG Runner
java
package runners;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
import org.testng.annotations.DataProvider;
@CucumberOptions(
features = "src/test/resources/features",
glue = {"stepdefs", "hooks"},
tags = "@smoke or @login",
plugin = {"pretty", "html:target/cucumber-report.html", "json:target/cucumber.json"}
)
public class TestRunner extends AbstractTestNGCucumberTests {
@Override
@DataProvider(parallel = true)
public Object[][] scenarios() {
return super.scenarios();
}
}
bash
mvn test
mvn test -Dcucumber.filter.tags="@smoke"
DataTables in Steps
gherkin
When I add these products to the cart:
| name |
| Sauce Labs Backpack |
| Sauce Labs Bike Light |
java
@When("I add these products to the cart:")
public void addProducts(io.cucumber.datatable.DataTable table) {
for (String name : table.asList("name")) {
inventory.addToCart(name);
}
}
Python Behave vs Java Cucumber
| Behave (Python) | Cucumber (Java) | |
|---|---|---|
| Steps | @given, @when, @then decorators | @Given, @When, @Then annotations |
| Hooks | environment.py | @Before, @After classes |
| Runner | behave command | TestNG or JUnit runner class |
| Parallel | Needs plugins or separate runs | TestNG DataProvider(parallel=true) |
Use BDD with purpose: BDD adds a layer of text to maintain. Choose it when the business team reads scenarios, otherwise normal TestNG or pytest tests are faster to write.
Conclusion
Cucumber for Java uses the same Gherkin features, Java step definitions, @Before/@After hooks and a TestNG runner class with tags and plugins for reports.
Quick Summary: feature files + step defs + hooks + TestNG runner.

