Guide
Parameterizing Cucumber Steps with Data Tables in Java
Learn how to feed multiple data rows into a single Cucumber step using Data Tables, with Java example, prerequisites, checks, and recovery tips.
Published by Tasadduq Burney
08 Oct 2025, 02:59 UTC
2 min75.7K views0

Desired outcome
Use a Cucumber Data Table to feed multiple data rows into a single step definition, so the test iterates over each row without duplicating steps.
Prerequisites
- Cucumber‑JVM 6.x or later
- Java 8+ JDK
- Maven or Gradle build tool
- Step definition classes in the
io.cucumber.java.enpackage
Procedure
- Add the dependency (Maven example)
- Write a feature step with a Data Table using pipe syntax.
- In the step definition, accept
io.cucumber.datatable.DataTableand convert rows to a list of maps or POJOs. - Run the test; Cucumber will execute the step once and iterate over each row internally.
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.15.0</version>
<scope>test</scope>
</dependency>
Given I calculate the sum of the following numbers:
| a | b |
| 2 | 3 |
| 5 | 7 |
| -1 | 4 |
@Given("I calculate the sum of the following numbers:")
public void calculateSum(DataTable table) {
List> rows = table.asMaps(Integer.class, Integer.class);
for (Map row : rows) {
int a = row.get("a");
int b = row.get("b");
int sum = a + b;
// Assume a helper method under test
int result = Calculator.add(a, b);
Assertions.assertEquals(sum, result, "Mismatch for a=" + a + " b=" + b);
}
}
Expected checks
- The console output shows the step executed once, with the data table printed (e.g.,
Given I calculate the sum of the following numbers: | a | b | ...). - No
UndefinedStepwarnings appear. - The build passes if all assertions succeed.
- In the Cucumber HTML report the step displays a table icon and each row is marked passed or failed individually.
Limitations and practical verification
- Header names must exactly match the keys used in
asMaps; mismatched headers cause mapping errors. - Avoid empty cells or extra whitespace around pipes; they can produce null values or parsing failures.
- To verify, run
mvn test(orgradle test) and inspect the console for the step execution line. Then opentarget/cucumber-reports/cucumber.html(or the equivalent Gradle path) and confirm the step shows a table icon with per‑row status.
Recovery options
- If a single row fails, wrap the iteration in a
try‑catchblock, log the failing data, and continue so other rows still run. - Alternatively, use Cucumber’s retry plugin to rerun failing scenarios.
- For more granular JUnit‑style reporting, convert the table to a list and drive the test with JUnit 5’s
@ParameterizedTestinstead of looping inside the step.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.