Skip to main content

Glob Patterns

A glob pattern in @Gherkin2JUnit lets a single marker class drive many feature files — one generated test class per feature, all extending the same marker.

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

What this demonstrates

  • @Gherkin2JUnit("specs/**/*.feature") matches all .feature files recursively
  • One marker class → one generated test class per discovered feature
  • All generated classes extend the same marker
  • Step methods organised into interfaces (CartSteps, LoginSteps, …) by domain
  • The marker class implements every step interface — shared steps are inherited everywhere
  • Hierarchical layout (specs/cart/, specs/user/) is preserved

Directory layout

src/test/resources/
└── specs/
├── cart/
│ ├── AddToCart.feature
│ └── Checkout.feature
└── user/
├── Login.feature
└── Registration.feature

src/test/java/.../glob/
├── AllFeatures.java ← single marker class with glob pattern
└── steps/
├── CartSteps.java ← interface — cart step methods
├── CheckoutSteps.java ← interface — checkout step methods
├── LoginSteps.java ← interface — login step methods
└── RegistrationSteps.java ← interface — registration step methods

What gets generated

AllFeatures.java
├→ AddToCartScenarios.java (from specs/cart/AddToCart.feature)
├→ CheckoutScenarios.java (from specs/cart/Checkout.feature)
├→ LoginScenarios.java (from specs/user/Login.feature)
└→ RegistrationScenarios.java (from specs/user/Registration.feature)

Each generated class is independent and runnable on its own.

The step-interface pattern

Organise step methods by domain area using Java interfaces with default methods:

public interface CartSteps {
default void iHaveAnEmptyShoppingCart() { /* shared impl */ }
default void iAdd$p1ToTheCart(String item) { /* shared impl */ }
}

Then the marker class implements every interface:

@Gherkin2JUnit("specs/**/*.feature")
public abstract class AllFeatures
implements CartSteps, CheckoutSteps, LoginSteps, RegistrationSteps {
}

Every generated *Scenarios class inherits the same step implementations through AllFeatures — no per-feature wiring required.

Run it

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