本文介绍了@Before不会在java Cucumber Step中执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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始终为null。永远不会调用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可以在以下网址查看:

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

推荐答案


  1. 确保您使用 cucumber.annotation.Before 而不是 org.junit.Before 。黄瓜不会处理JUnit注释。 (有关的Scenario Hooks部分的更多信息。 )

  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.

这篇关于@Before不会在java Cucumber Step中执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 12:47