Skip to main content

Cucumber Data Tables

The CUCUMBER_DATA_TABLE mode — Gherkin data tables are passed as Cucumber DataTable objects, giving you the full Cucumber DataTable API for conversions and POJO mapping.

Source: examples/common-use-cases/example-2 on GitHub.

What this demonstrates

  • @Gherkin2JUnitOptions(dataTableParameterType = CUCUMBER_DATA_TABLE) switches the mode
  • Step methods receive DataTable instead of List<XxxParam>
  • The generator emits a createDataTable(String tableLines) helper that parses pipe-delimited text blocks
  • You must provide a getTableConverter() method somewhere in the class hierarchy
  • Access to the full Cucumber API: asList(), asMap(), asMaps(), POJO mapping
  • Requires the cucumber-java dependency

When to pick this mode

AspectLIST_OF_OBJECT_PARAMS (default)CUCUMBER_DATA_TABLE
Parameter typeList<GeneratedParam>DataTable
Type safetyCompile-time (typed fields)Runtime (string-based)
POJO mappingAutomatic (generated classes)Manual (DataTableTypeRegistry)
DependenciesNonecucumber-java
Best forNew projects, compile-time safetyMigrating from Cucumber, complex conversions

Generated call site

myCartContainsTheFollowingProducts(createDataTable("""
|name |qty|unit price|
|Wireless Headphones|1 |59.99 |
|Coffee Beans 1kg |3 |12.50 |
"""));

POJO mapping with DataTableTypeRegistry

registry.defineDataTableType(new DataTableType(
Product.class,
(Map<String, String> row) -> new Product(
row.get("name"),
Integer.parseInt(row.get("qty")),
Double.parseDouble(row.get("unit price"))
)
));

// Inside the step method:
List<Product> products = dataTable.asList(Product.class);

Run it

cd examples/common-use-cases/example-2
mvn test