Skip to main content

Config Inheritance

Share generation options across many marker classes via @Gherkin2JUnitOptions on a base class. Override specific options per marker as needed.

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

What this demonstrates

  • @Gherkin2JUnitOptions on a base class applies to every marker class that extends it
  • The annotation is @Inherited — child classes pick up all options automatically
  • A child class with its own @Gherkin2JUnitOptions overrides only the options it explicitly sets — unspecified options keep their inherited value
  • One base class can standardise generation behaviour across a whole project

Class hierarchy

BaseFeature.java                          (@Gherkin2JUnitOptions — shared options)
├── ShoppingCartFeature.java (inherits all options)
│ └→ ShoppingCartScenarios.java (generated — abstract, keyword prefixes)
└── CheckoutFeature.java (@Gherkin2JUnitOptions — overrides one option)
└→ CheckoutScenarios.java (generated — abstract, keyword prefixes)

How options flow

@Gherkin2JUnitOptions(
useStepKeywordInStepMethodName = true,
tagForEmptyScenarios = "todo",
tagForEmptyRules = "todo"
)
public abstract class BaseFeature {
}

@Gherkin2JUnit("specs/ShoppingCart.feature")
public abstract class ShoppingCartFeature extends BaseFeature {
// Inherits everything from BaseFeature
}

@Gherkin2JUnit("specs/Checkout.feature")
@Gherkin2JUnitOptions(addSourceLineNumbers = true) // only this is locally set
public abstract class CheckoutFeature extends BaseFeature {
// Inherits useStepKeywordInStepMethodName, tagForEmptyScenarios, tagForEmptyRules
// Locally adds addSourceLineNumbers
}
OptionBaseFeatureShoppingCartFeatureCheckoutFeature
useStepKeywordInStepMethodNametrueinherited (true)inherited (true)
tagForEmptyScenarios"todo"inherited ("todo")inherited ("todo")
tagForEmptyRules"todo"inherited ("todo")inherited ("todo")
addSourceLineNumbersdefault (false)inherited (false)true (overridden)

Effect on the generated code

Because useStepKeywordInStepMethodName = true is inherited, both generated classes use Gherkin-keyword-prefixed method names:

// ShoppingCartScenarios (inherits everything)
public abstract void givenIHaveAnEmptyShoppingCart();
public abstract void whenIAdd$p1ToTheCart(String p1);
public abstract void thenTheCartShouldContain$p1Item(Integer p1);

Run it

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