Java 钩子不在黄瓜4中运行

Java 钩子不在黄瓜4中运行,java,cucumber,Java,Cucumber,@运行Runner类时,钩子的Before和@After方法未运行 我正在使用依赖项: 黄瓜java 4.3.0 黄瓜jvm 4.3.0 stepdef文件中的所有步骤都正常运行,但挂钩除外。最新的黄瓜版本有什么问题吗 public class Hooks { @Before public void beforeHooks() { System.out.println("Run Before Scenario"); } @After public void afterHooks() {

@运行Runner类时,钩子的Before和@After方法未运行

我正在使用依赖项: 黄瓜java 4.3.0 黄瓜jvm 4.3.0

stepdef文件中的所有步骤都正常运行,但挂钩除外。最新的黄瓜版本有什么问题吗

public class Hooks {
@Before
public void beforeHooks() {
    System.out.println("Run Before Scenario");
}

@After
public void afterHooks() {
    System.out.println("Run After Scenario");
}

首先确保您使用的是cucumber.api.java.Before(接口),而不是org.junit.Before,因为cucumber不会处理junit注释

  • @Before-导入cucumber.api.java.Before
  • @After-导入cucumber.api.java.After
希望我们在这方面意见一致,让我们毫不拖延地向前迈进

Second让我们了解,如果步骤实现方法和钩子类在同一个包中,那么我们不需要在runner的glue选项中另外指定钩子类的路径。在我的例子中,两个包在同一个包中,所以我们只需要设置一个包

但是如果它们在不同的包中,那么请将Hooks类的包包含在runner文件的glue选项中

黄瓜跑步者:

package com.jacksparrow.automation.suite.runner;

import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(features = "classpath:features/functional/",
                     glue = {"com.jacksparrow.automation.steps_definitions.functional" },
                   plugin = { "pretty","json:target/cucumber-json/cucumber.json",
                            "junit:target/cucumber-reports/Cucumber.xml", "html:target/cucumber-reports"},
                   tags = { "@BAMS_Submitted_State_Guest_User" },
                   strict = false,
                   dryRun = false,
               monochrome = true)

public class RunCukeTest {
}
关键点:我们不会将直接依赖项和传递依赖项特别是它们的版本混为一谈!这样做可能会导致不可预测的结果。您可以添加以下最小依赖项集

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>4.3.0</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-picocontainer</artifactId>
    <version>4.3.0</version>
    <scope>test</scope>
</dependency>

黄瓜
黄瓜刺柏
4.3.0
测试
黄瓜
黄瓜皮容器
4.3.0
测试

首先确保使用的是cucumber.api.java.Before(接口),而不是org.junit.Before,因为cucumber不会处理junit注释

  • @Before-导入cucumber.api.java.Before
  • @After-导入cucumber.api.java.After
希望我们在这方面意见一致,让我们毫不拖延地向前迈进

Second让我们了解,如果步骤实现方法和钩子类在同一个包中,那么我们不需要在runner的glue选项中另外指定钩子类的路径。在我的例子中,两个包在同一个包中,所以我们只需要设置一个包

但是如果它们在不同的包中,那么请将Hooks类的包包含在runner文件的glue选项中

黄瓜跑步者:

package com.jacksparrow.automation.suite.runner;

import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(features = "classpath:features/functional/",
                     glue = {"com.jacksparrow.automation.steps_definitions.functional" },
                   plugin = { "pretty","json:target/cucumber-json/cucumber.json",
                            "junit:target/cucumber-reports/Cucumber.xml", "html:target/cucumber-reports"},
                   tags = { "@BAMS_Submitted_State_Guest_User" },
                   strict = false,
                   dryRun = false,
               monochrome = true)

public class RunCukeTest {
}
关键点:我们不会将直接依赖项和传递依赖项特别是它们的版本混为一谈!这样做可能会导致不可预测的结果。您可以添加以下最小依赖项集

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>4.3.0</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-picocontainer</artifactId>
    <version>4.3.0</version>
    <scope>test</scope>
</dependency>

黄瓜
黄瓜刺柏
4.3.0
测试
黄瓜
黄瓜皮容器
4.3.0
测试

请添加pom.xml代码您是否将包含Hooks类的包包含在runner的glue选项中?正在尝试逐步为您提供以下解决方案。谢谢@Grasshopper。在glue中添加路径也起到了作用。请添加pom.xml代码您是否在runner的glue选项中包含了包含Hooks类的包?正在尝试逐步为您提供以下解决方案。谢谢@Grasshopper。在胶水中添加路径也有效。非常感谢。第二个解决方案是这个问题。hooks类与steps在一个不同的包中。现在应该使用io.cumber.java.Before进行
@Before
注释,使用io.cumber.java.After进行
@Before
注释。非常感谢。第二个解决方案是这个问题。hooks类与steps在一个不同的包中。现在,您应该将io.cumber.java.Before用于
@Before
注释,将io.cumber.java.After用于
@After
注释。