@Before 不在 java Cucumber Step 中执行

2022-01-22 00:00:00 bdd cucumber java

我有一个 Cucumber Step 类,我正在尝试为所有场景初始化页面模型.所以我添加了一个@Before 注释方法:

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);
}

然后,我得到了一堆依赖于设置 loginPage 的步骤.例如

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();
}

我有多个 Step 课程.上述两种方法都在同一个 Step 类中.但是 loginPage 始终为空.beforeScenario 方法永远不会被调用.我是否完全误解了 @Before 的工作原理?关于如何获得我想要的工作的任何提示?

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?

我还有一个 @After 注释方法,它会按预期在每个场景之后运行.

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

Pom 可见于:http://pastebin.com/PJ6qQRK9

推荐答案

  1. 确保您使用的是 cucumber.annotation.Before 而不是 org.junit.Before.Cucumber 不会处理 JUnit 注释.(更多信息请参见这篇博文的场景挂钩部分.)

  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.)

确保您的@Before 方法是public,而不是private.

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

相关文章