Skip to main content

Convention Discovery

@Gherkin2JUnit with no path argument — the processor finds feature files by convention, looking in the same package as the marker class.

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

What this demonstrates​

  • A bare @Gherkin2JUnit (no value) triggers convention-based discovery
  • The processor scans for .feature / .specb files in the same package as the annotated class
  • Feature files can live in src/test/java next to their marker class — easy to navigate in the IDE
  • A concrete test in the same package implements the generated ShoppingCartScenarios class
  • Maven testResources configuration is required to copy the feature files to the classpath

Directory layout​

src/test/java/
└── dev/specbinder/examples/gettingstarted/colocated/
├── ShoppingCart.java ← marker class (@Gherkin2JUnit)
├── ShoppingCart.specb ← co-located spec file
└── ShoppingCartTest.java ← runnable scenario implementation

The marker, spec, and runnable test share the same package — one click to jump between them in your IDE.

The marker class​

ShoppingCart.java
package dev.specbinder.examples.gettingstarted.colocated;

import dev.specbinder.annotations.Gherkin2JUnit;

@Gherkin2JUnit
public abstract class ShoppingCart {
}

The runnable test​

SpecBinder generates ShoppingCartScenarios in the same package. A concrete test beside the spec extends that class and implements its step methods with ordinary Java and JUnit assertions:

ShoppingCartTest.java
package dev.specbinder.examples.gettingstarted.colocated;

public class ShoppingCartTest extends ShoppingCartScenarios {
private final List<String> items = new ArrayList<>();

@Override
public void iHaveAnEmptyShoppingCart() {
items.clear();
}

@Override
public void iAdd$p1ToTheCart(String item) {
items.add(item);
}

@Override
public void theCartShouldContain$p1Item(Integer expectedCount) {
assertEquals(expectedCount, items.size());
}

// The remaining generated step methods are implemented in the same way.
}

Required Maven configuration​

To make Maven copy .feature / .specb files from src/test/java to the test classpath:

<build>
<testResources>
<testResource>
<directory>src/test/java</directory>
<includes>
<include>**/*.feature</include>
<include>**/*.specb</include>
</includes>
</testResource>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
</build>

Without this, Maven won't include the co-located feature files on the classpath and the processor won't find them.

Run it​

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

Both scenarios should pass.