Skip to main content

Scenario Outline

Scenario Outline with an Examples table maps to JUnit's @ParameterizedTest + @CsvSource.

Source: examples/getting-started/example-6 on GitHub.

What this demonstrates

  • Scenario Outline@ParameterizedTest (instead of @Test)
  • Examples table rows → @CsvSource(textBlock = …) data
  • Placeholders (<name>, <price>) become method parameters
  • Column headers become parameter names (sanitised to valid Java identifiers — expected subtotalexpectedSubtotal)
  • Type inference is applied per column across all rows
  • Multiple Examples blocks → multiple repeatable @CsvSource annotations on the same method

Gherkin → JUnit mapping

GherkinJUnit
Scenario Outline:@ParameterizedTest
Examples: table@CsvSource(textBlock = …)
<placeholder> in stepsMethod parameter
Column headerParameter name
Table rowOne test iteration

Generated output

@ParameterizedTest(name = "Example {index}: [{arguments}]")
@CsvSource(
useHeadersInDisplayName = true,
delimiter = '|',
textBlock = """
name | start qty | price | new qty | expected subtotal
Wireless Headphones | 1 | 60.00 | 2 | 120.00
Coffee Beans 1kg | 2 | 15.50 | 3 | 46.50
USB-C Cable | 1 | 8.99 | 5 | 44.95
"""
)
@DisplayName("Scenario Outline: Subtotal updates when quantity changes")
public void scenario_1(String name, Integer startQty, Double price,
Integer newQty, Double expectedSubtotal) {
myCartContains$p1WithQuantity$p2AndUnitPrice$p3(name, startQty, price);
iChangeTheQuantityTo$p1(newQty);
theCartSubtotalShouldBe$p1(expectedSubtotal);
}

When multiple Examples blocks are present, each block produces its own @CsvSource annotation on the same @ParameterizedTest method (Jupiter treats them as repeatable).

Run it

cd examples/getting-started/example-6
mvn test