Search code examples
javacucumberbdd

@Before doesn't execute in java Cucumber Step


I've got a Cucumber Step class that i'm attempting to initialise a page model for all scenarios. So I added a @Before annotated method :

@Before()
private void beforeScenario() {
    LOGGER.info("Running before!");
    loginPage = BrowserDriver.getPageModel(LoginPage.class);
}

I've then got a bunch of steps that rely on loginPage being set. e.g.

@When("^I click the help link$")
public void I_click_the_help_link() {
    loginPage.clickHelpLink();
}

I have multiple Step classes. Both of the methods above are in the same same Step class. However loginPage is always null. The beforeScenario method is never being called. Have I completely misunderstood how @Before is meant to work? Any tips on how to get what I want to work?

Edit : I also have an @After annotated method that does get run after every scenario as expected.

Edit : Pom can be seen at : http://pastebin.com/PJ6qQRK9


Solution

    1. Make sure you are using cucumber.annotation.Before rather than org.junit.Before. Cucumber will not process JUnit annotations. (More information in the Scenario Hooks section of this blog post.)

    2. Make sure your @Before method is public, not private.