我尝试添加Cucumber来测试我的Java项目,我写了一个功能文件:
Feature: I visit the website
Scenario: I read the home page
Given I am on the "home" page
我写了一个课程来运行cukes
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.
答案 0 :(得分:3)
您没有遗漏注释。您缺少类路径中的内容。
我将从简化设置位开始。您正在使用Maven,因此我将示例功能文件放在与RunCukesTest类相同的包中。如果RunCukesTest位于com.myproject.test包中,那么这意味着:
的src /测试/资源/ COM / myproject的/测试
我认为您目前正在为您的功能文件使用src / test / features。这不是Maven包含在类路径中的路径。除非您明确添加,否则您不会向我们展示,如果您这样做的话。
移动要素文件应该允许您删除@CucumberOptions并删除一个潜在的问题来源。
然后,我将在与RunCukesTest相同的包中的类中实现该步骤。我会使用运行构建时提供的代码片段,mvn测试。最后做system.out.println()你必须真正知道预期的方法确实被执行了。这些片段可能是Java 8之前的做事方式。
有了这个工作,我会尝试让Java 8步骤正常工作。
更多细节可以在Cucumber的源代码中看到: https://github.com/cucumber/cucumber-jvm/tree/master/java8/src/test/java/cucumber/runtime/java8/test
答案 1 :(得分: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>
您可能需要更新版本。
答案 2 :(得分: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
。也不确定为什么在步骤定义中添加String参数,请删除并尝试。