我尝试添加 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。