Skip to content

Why does cucumber run @Before in all glue code files

I have a problem with my cucumber tests. It runs the @Before method in all the glue classes.

For example. This feature file have one glue code in the MainStepDef.class.

#language: en
Feature: Testing a feature
  Test before method

    Given stuff is created

  Scenario: The test
    When i do stuff
    Then stuff will be done

The MainStepDef:

public class MainStepDef {

    public void setup() {
        System.out.println("This is OK!");

    @Given("^stuff is created$")
    public void stuff_is_created() throws Throwable {

    @When("^i do stuff$")
    public void i_do_stuff() throws Throwable {

    @Then("^stuff will be done$")
    public void stuff_will_be_done() throws Throwable {


I have an additional glue file called: OtherStep.class

public class OtherStepDef {

    public void setup() {
        throw new RuntimeException("No! Bad code! BAD CODE!");

    @Given("^some other stuff is also created$")
    public void some_other_stuff_is_also_created() throws Throwable {

And finally I have my runner class.

@CucumberOptions(strict = true, tags = {"@run", "~@ignore" }, 
        format = {"html:target/systemtest", "pretty" }, features = "classpath:features/",
        glue = {"com.sorkmos.stepdef" }, monochrome = true)
public class RunFeatures {


When I run this I get the runtime exception from the OtherStepDef setup method.

Why does this happen? Should it not execute only the glue needed for the feature?

Example project:



This is the intended behaviour of Cucumber:!topic/cukes/7gILvMsE2Js

This is the intended behaviour of the @Before and @After hooks: they are not related to each step definition. Every hook is run on each scenario (unless it’s filtered out by tags).

9 People found this is helpful