Hook tag specificity versus declaration order in Cucumber execution
0 reputation · 15 Jul 2023, 14:50 UTC
0 reputation · 15 Jul 2023, 14:50 UTC
When Cucumber evaluates multiple @Before or @After hooks matching a scenario tag, the framework applies tag specificity to determine execution precedence. Documented behavior states that more specific tags override generic ones, yet among hooks sharing equal specificity, execution follows declaration order within the same step definition file. This inquiry identifies whether declaration order applies across independently loaded step definition files or remains file-local, and how tag hierarchy affects specificity ranking in multi-module Cucumber projects.
Does Cucumber merge declaration sequences from all loaded step definition files when tag specificity is equal? How does the @tagA versus @tagA@specific hierarchy resolve specificity conflicts between hooks in different modules? Is the intra-file ordering rule intentionally scoped, and what governs inter-file hook execution precedence?
35270 reputation · 15 Jul 2023, 21:56 UTC
Cucumber‑JVM does **not** use tag specificity to order {@code @Before} or {@code @After} hooks. When multiple hooks match a scenario, precedence is determined first by the {@code order} attribute on the hook annotation, and only when the {@code order} values are equal does the framework fall back to the order in which glue classes are registered/loaded. This registration order is not a guaranteed public contract and can vary between build tools, IDEs, or CI environments.
The {@code @Before} and {@code @After} annotations accept a tag expression solely for **matching** scenarios. The Cucumber‑JVM source (io.cucumber.java.Before/After) shows that the annotation only stores the expression and an {@code order} integer; there is no code that compares tag expressions to compute a priority. Therefore the idea that “more specific tags override generic ones” is not a documented ordering mechanism.
If you are relying on the default {@code order} (=10000) and observing inconsistent hook execution across environments, confirm the exact Cucumber‑JVM version on the classpath (e.g., via {@code mvn dependency:tree} or {@code gradle dependencies}). The default {@code order} value and step‑level hook support have remained stable since version 2.0, but verifying the version removes any uncertainty about potential changes.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.