Cucumber JVM 7.x step definition visibility: restricting public steps to their own module
0 reputation · 15 Feb 2023, 10:36 UTC
0 reputation · 15 Feb 2023, 10:36 UTC
In multi-module JVM projects using Cucumber JVM 7.x, step definition methods must be public for the glue backend to discover them, whether they use Cucumber Expressions or regular expressions. Package-private or private step methods are ignored during discovery, and the affected steps surface as undefined at runtime rather than as compile-time errors.
The design goal is to keep a module's steps usable within that module while preventing features in unrelated modules from matching the same step text through classpath-wide glue scanning. The @CucumberOptions glue attribute limits which packages are scanned, but any public step method on the test classpath remains callable from any feature that shares the glue configuration, and Cucumber 7.x documents no option to enable non-public step definitions.
Cucumber JVM 7.x can only discover step definition methods that are public. There is no configuration or annotation that makes non‑public methods visible, so the only supported way to keep a module’s steps private to that module is to limit the glue packages scanned by the runner and to keep any helper steps outside those packages (or in a separate test‑jar).
public methods annotated with @Given, @When, @Then (or using Cucumber Expressions). Package‑private or private methods are skipped silently.@CucumberOptions glue attribute (or element) determines which packages are scanned for step definitions. No other mechanism exists to restrict visibility.@CucumberOptions(glue = "com.example.mymodule.steps") so only that module’s step package is scanned.com.example.mymodule.steps.internal) that is **not** listed in any module’s glue, or ship them in a separate classifier/jar that is not added to the test classpath.Undefined in the console/report.@RunWith(Cucumber.class)
@CucumberOptions(
features = "classpath:features",
glue = "com.example.mymodule.steps",
plugin = {"pretty", "html:target/cucumber-reports"}
)
public class MyModuleRunner {}
If you need to confirm whether a particular runner is using XML instead of annotations, let me know which configuration style you are using so I can adjust the example.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.