Skip to main content

Step Parameters

How quoted values in Gherkin steps become typed Java method parameters with automatic type inference.

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

What this demonstrates​

  • Each quoted value in a step ("59.99", "3", "true") becomes a method parameter
  • The generator infers the parameter type from the value's shape
  • Parameter slots are marked in the method name with $p1, $p2, $p3 placeholders
  • Multiple parameters in a single step
  • The concrete test class receives the values already typed and uses them directly, with no parsing

Type inference rules​

Quoted valueInferred type
"true" / "false"Boolean
Integer literal (e.g. "3")Integer (or Long if too large)
Decimal literal (e.g. "59.99")Double
Single character (e.g. "Y")Character
Everything else (e.g. "Wireless Headphones")String

The feature file​

src/test/resources/specs/ShoppingCart.feature
Scenario: Add item with quantity and price
Given my cart contains "Wireless Headphones" with quantity "1" and unit price "59.99"
When I change the quantity to "3"
Then the cart subtotal should be "179.97"

Scenario: Apply a discount flag
Given my cart contains "Coffee Beans" with quantity "2" and unit price "15.50"
And the discount applied is "true"
When I view the cart summary
Then the discount badge shows "Y"

The generated step methods​

public abstract void myCartContains$p1WithQuantity$p2AndUnitPrice$p3(String p1, Integer p2, Double p3);

public abstract void iChangeTheQuantityTo$p1(Integer p1);

public abstract void theCartSubtotalShouldBe$p1(Double p1);

public abstract void theDiscountAppliedIs$p1(Boolean p1);

public abstract void iViewTheCartSummary();

public abstract void theDiscountBadgeShows$p1(Character p1);

Each "…" token in the original step text becomes a parameter; the $p1, $p2, … placeholders in the method name mark where each parameter belongs. Identical step text under different keywords (Given / When / Then / And / But) reuses the same method.

The test class​

ShoppingCartTest implements the abstract methods, so the step code works with real types:

ShoppingCartTest.java
@Override
public void iChangeTheQuantityTo$p1(Integer newQuantity) {
quantity = newQuantity;
}

@Override
public void theCartSubtotalShouldBe$p1(Double expectedSubtotal) {
assertEquals(expectedSubtotal, quantity * unitPrice, 0.001);
}

@Override
public void theDiscountAppliedIs$p1(Boolean applied) {
discountApplied = applied;
}

@Override
public void theDiscountBadgeShows$p1(Character expectedBadge) {
assertEquals(expectedBadge, discountBadge);
}

Parameter names in your implementation can be anything — only the method name and parameter types need to match the generated signature.

Run it​

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