2

我尝试添加 Cucumber 来测试我的 Java 项目,我写了一个功能文件:

Feature: I visit the website

    Scenario: I read the home page
        Given I am on the "home" page

我写了一个类来运行杯子

import cucumber.api.CucumberOptions;

/**
 *  See https://cucumber.io/docs/reference/jvm#junit-runner
 */
@RunWith(Cucumber.class)
@CucumberOptions(glue = {"classpath:stepdefinitions"},features = {"src/test/features"})
public class RunCukesTest {
}

以及步骤定义:(我觉得我在这里缺少注释)

package com.myproject.test.stepdefinitions;

import cucumber.api.PendingException;
import cucumber.api.java8.En;

public class WebSteps implements En {

    public WebSteps() {
        Given("^I am on the \"([^\"]*)\" page$", (String page) -> {
            System.out.println(page);
        });
    }
}

因此,无论我输入什么glue = {},都找不到该步骤。有glue = {"classpath:stepdefinitions"}glue = {"stepdefinitions"}测试是绿色的,我有“您可以使用下面的代码片段实现缺少的步骤”消息(未找到该步骤)。如果我放置完整的包路径,glue = {"com.myproject.test.stepdefinitions"}或者glue = {"classpath:com.myproject.test.stepdefinitions"}

我有一个堆栈跟踪:

java.lang.NoClassDefFoundError: org/springframework/test/context/ContextHierarchy
    at cucumber.runtime.java.spring.SpringFactory.annotatedWithSupportedSpringRootTestAnnotations(SpringFactory.java:198)
    at cucumber.runtime.java.spring.SpringFactory.dependsOnSpringContext(SpringFactory.java:187)
    at cucumber.runtime.java.spring.SpringFactory.addClass(SpringFactory.java:61)
    at cucumber.runtime.java.JavaBackend.loadGlue(JavaBackend.java:96)
    at cucumber.runtime.Runtime.<init>(Runtime.java:91)
    at cucumber.runtime.Runtime.<init>(Runtime.java:69)
    at cucumber.runtime.Runtime.<init>(Runtime.java:65)
    at cucumber.api.junit.Cucumber.createRuntime(Cucumber.java:78)
    at cucumber.api.junit.Cucumber.<init>(Cucumber.java:58)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:29)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:21)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:84)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.ClassNotFoundException: org.springframework.test.context.ContextHierarchy
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 26 more

这是我在我的添加pom.xml

    <!-- Tests with cucumber -->
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java8</artifactId>
        <version>1.2.3</version>
        <scope>test</scope>
    </dependency>
    <!-- Dependency Injection with Spring https://cucumber.io/docs/reference/java-di#spring -->
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-spring</artifactId>
        <version>1.2.3</version>
        <scope>test</scope>
    </dependency>
    <!-- JUnit Runner : https://cucumber.io/docs/reference/jvm#running -->
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>1.2.3</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

我的项目使用 Spring 4。

4

3 回答 3

3

您不会缺少注释。您的类路径中缺少内容。

我将从简化设置位开始。您正在使用 Maven,因此我会将示例功能文件放在与 RunCukesTest 类相同的包中。如果 RunCukesTest 位于 com.myproject.test 包中,那么这是否意味着:

src/test/resources/com/myproject/test

我认为您目前正在为您的功能文件使用 src/test/features 。这不是 Maven 包含在类路径中的路径。除非您明确添加它,否则不会向我们展示您是否添加。

移动功能文件应该允许您删除 @CucumberOptions 并删除一个潜在的问题来源。

然后,我将在与 RunCukesTest 相同的包中的类中实现该步骤。我会使用您运行构建时提供的代码片段 mvn test。最后执行 system.out.println() 你必须真正知道预期的方法确实被执行了。这些片段可能是 Java 8 之前的处理方式。

有了这个工作,我会尝试让 Java 8 步骤工作。

更多细节可以在 Cucumber 的源代码中看到: https ://github.com/cucumber/cucumber-jvm/tree/master/java8/src/test/java/cucumber/runtime/java8/test

于 2016-01-27T08:07:33.697 回答
0

将插件添加到 Runnerclass plugin={"pretty","html:format"}。我使用如下:

@RunWith(Cucumber.class)
@CucumberOptions(
  plugin={"pretty","html:format"},      
  features = "Features/name.feature",glue={"stepdefinitions path in eclipse"})

public class RunnerClass {
}

当你想参数化HOME变量时,它应该像< Home >而不是Home. 也不确定为什么在步骤定义中添加字符串参数,请删除并尝试。

于 2017-04-11T10:39:33.570 回答
0

该错误表明您缺少 Spring 依赖项;您必须先解决此问题,然后再调查未找到步骤定义的原因。你还没有在你的项目中发布完整的 deps 列表,但你至少应该在你的 pom 中有这些依赖项:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>4.1.6.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context-support</artifactId>
  <version>4.1.6.RELEASE</version>
</dependency>

您可能需要更新版本。

于 2016-01-27T09:04:23.247 回答