@Before doesn't execute in java Cucumber Step

こ雲淡風輕ζ 提交于 2019-12-03 11:58:17

问题


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


回答1:


  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.




回答2:


Hello I know that is an old post, but none of these solutions work for me. So I'm going to share my solution.

I created the class Hooks under the package: com.mycompany.automation.util

package com.mycompany.automation.util;

import com.mycompany.automation.rest.database.AS400DBManager;
import cucumber.api.java.After;
import java.sql.SQLException;

/**
 * @author <a href="mesaj@mycompany.com">Julian Mesa</a>
 * @version 0.1.0
 * @since 0.1.0
 */

    public class Hooks {

      @After
      public void beforeScenario() throws SQLException, ClassNotFoundException {
        System.out.print("Closing connection.");
        AS400DBManager.getInstance().closeConnection();
      }

    }

and then I specified the package in the glue options in the runner:

@RunWith(CucumberWithSerenity.class)
@CucumberOptions(
    features = "src/test/resources/features",
    glue = {"com.mycompany.automation.features.steps",
        "com.mycompany.automation.util"}
)

And it worked.




回答3:


There is an accepted answer to this question, but I wanted to point out the comment made by Matt Watson which solved the issue for me and for which I have not seen similar advice elsewhere:

I've had a play about with some of my cucumber-jvm tests and I think I've spotted it. Your @Before method should be public rather than private

The @Before method must be public.




回答4:


In my case worked the addition of a package where @Before was defined to the glue parameter:

@CucumberOptions(glue = {"cucumber.hook", "cucumber.steps"})



回答5:


I know this issue is old, but in case somebody runs into the same problem using IntelliJ:

Check the "Glue"-Property in your Run/Debug Configuration. This is the list of paths that Glue (the autowirering-system from Cucumber) uses to find Classes to wire.
It seems like IntelliJ is generating this property, if it is not specifically defined in the the Template for "Cucumber Java"-Configs.

I don't know how it is generated, but for me the package that contains my class with the Before-Method in question did not exist. After adding it everything worked normally.



来源:https://stackoverflow.com/questions/24029341/before-doesnt-execute-in-java-cucumber-step

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!