@Before 不在 java Cucumber Step 中执行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24029341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
@Before doesn't execute in java Cucumber Step
提问by Peter Wilkinson
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 :
我有一个 Cucumber Step 类,我正在尝试为所有场景初始化页面模型。所以我添加了一个@Before 注释方法:
@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.
然后我有一堆依赖于 loginPage 设置的步骤。例如
@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?
我有多个 Step 类。以上两种方法都在同一个 Step 类中。但是 loginPage 始终为空。beforeScenario 方法永远不会被调用。我是否完全误解了@Before 的工作原理?关于如何获得我想要的工作的任何提示?
Edit : I also have an @After annotated method that does get run after every scenario as expected.
编辑:我还有一个@After 注释的方法,它确实按预期在每个场景之后运行。
Edit : Pom can be seen at : http://pastebin.com/PJ6qQRK9
编辑:Pom 可以在以下位置看到:http: //pastebin.com/PJ6qQRK9
采纳答案by Matt Watson
Make sure you are using
cucumber.annotation.Before
rather thanorg.junit.Before
. Cucumber will not process JUnit annotations. (More information in the Scenario Hooks section of this blog post.)Make sure your @Before method is
public
, notprivate
.
确保您使用的是
cucumber.annotation.Before
而不是org.junit.Before
. Cucumber 不会处理 JUnit 注释。(更多信息请参见本博文的 Scenario Hooks 部分。)确保您的 @Before 方法是
public
,而不是private
。
回答by ndurante
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 bepublic
rather thanprivate
我玩了一些我的黄瓜 jvm 测试,我想我已经发现了。你的
@Before
方法应该是public
而不是private
The @Before
method mustbe public
.
该@Before
方法必须是public
.
回答by Julian Mesa
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
我在Hooks
包下创建了类: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="[email protected]">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:
然后我在 runner 的胶水选项中指定了包:
@RunWith(CucumberWithSerenity.class)
@CucumberOptions(
features = "src/test/resources/features",
glue = {"com.mycompany.automation.features.steps",
"com.mycompany.automation.util"}
)
And it worked.
它奏效了。
回答by agata
In my case worked the addition of a package where @Before was defined to the glue parameter:
在我的情况下,添加了一个包,其中 @Before 被定义为胶水参数:
@CucumberOptions(glue = {"cucumber.hook", "cucumber.steps"})
回答by Leo Hilbert
I know this issue is old, but in case somebody runs into the same problem using IntelliJ:
我知道这个问题很老,但如果有人使用 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.
检查运行/调试配置中的“胶水”属性。这是 Glue(来自 Cucumber 的自动装配系统)用来查找要连接的类的路径列表。
如果 IntelliJ 没有在“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.
我不知道它是如何生成的,但对我来说,包含我的类和有问题的 Before-Method 的包不存在。添加后一切正常。
Edit:
编辑:
Found out more background-info. The IntelliJ Cucumber Plugin does not use Junit oder TestNG, but his own implemented runner suite. This runner does NOT interepret the Annotation-Based configurations from Cucumber, only the ones from the Cucumber Property-File or System-Properties. The TestNG-Suite however always overwrites the Glue-Path, not matter if it was actually set or is present. The most consistent way I found was to configure it both using annotations and properties. That way you are always sure that you're config ist used (Gradle-Runner, TestNG-Runner, IntellijCucumber-Runner)
发现更多背景信息。IntelliJ Cucumber Plugin 不使用 Junit 或 TestNG,而是使用他自己实现的运行器套件。此运行程序不解释来自 Cucumber 的基于注释的配置,仅解释来自 Cucumber 属性文件或系统属性的配置。然而,TestNG-Suite 总是覆盖 Glue-Path,不管它是实际设置还是存在。我发现的最一致的方法是使用注释和属性来配置它。这样,您始终可以确保使用了配置(Gradle-Runner、TestNG-Runner、IntellijCucumber-Runner)
回答by dani198
I know this Is an old issue but I found a Solution using the 5.5.0 version of cucumber java: I added the Hooks class into the Steps package, having only the glue="Step classpath" and it worked fine for me :)
我知道这是一个老问题,但我找到了一个使用 5.5.0 版本的 Cucumber java 的解决方案:我将 Hooks 类添加到 Steps 包中,只有胶水 =“Step classpath”,它对我来说很好用 :)
Hope that helps!!
希望有帮助!!
回答by Francisco Guerrero Gil
In my case, it has worked when I put "dryRun = false" in the Cucumber options.
就我而言,当我在 Cucumber 选项中放置“dryRun = false”时它起作用了。