运行Cucumber JUnit测试时的InitializationError

时间:2015-11-13 20:03:49

标签: java eclipse junit cucumber bdd

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

@RunWith(Cucumber.class)
@CucumberOptions(
        format={"pretty"},
        features= "src/features/"
        )
public class cucumberRunner {

}

使用:

  • cucumber-core-1.2.4
  • cucumber-java-1.2.4
  • 黄瓜的junit-1.2.4
  • junit-4.12
  • Eclipse Mars.1
  • java 8

错误追踪:

  

java.lang.NoClassDefFoundError:org / dom4j / Element         at java.lang.Class.getDeclaredMethods0(Native Method)         at java.lang.Class.privateGetDeclaredMethods(Unknown Source)         at java.lang.Class.privateGetPublicMethods(Unknown Source)         at java.lang.Class.getMethods(Unknown Source)         在cucumber.runtime.java.MethodScanner.scan(MethodScanner.java:40)         在cucumber.runtime.java.JavaBackend.loadGlue(JavaBackend.java:86)         在cucumber.runtime.Runtime。(Runtime.java:91)         在cucumber.runtime.Runtime。(Runtime.java:69)         在cucumber.runtime.Runtime。(Runtime.java:65)         在cucumber.api.junit.Cucumber.createRuntime(Cucumber.java:78)         在cucumber.api.junit.Cucumber。(Cucumber.java:58)         at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)         at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)         at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown   资源)         at java.lang.reflect.Constructor.newInstance(Unknown Source)         在org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)         at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)         在org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)         在org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)         在org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)         at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)         在org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:84)         在org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70)         在org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)         在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)         在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)         在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)         在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)       引起:java.lang.ClassNotFoundException:org.dom4j.Element         at java.net.URLClassLoader.findClass(Unknown Source)         at java.lang.ClassLoader.loadClass(Unknown Source)         at sun.misc.Launcher $ AppClassLoader.loadClass(Unknown Source)         at java.lang.ClassLoader.loadClass(Unknown Source)         ......还有28个

可能是什么问题?

3 个答案:

答案 0 :(得分:4)

问题是你的班级位于黄瓜包中。 重命名包或将步骤定义和其他胶水代码移动到子包,例如' cucumber.steps'并限制胶水代码的查找到这个包:

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

 @RunWith(Cucumber.class)
 @CucumberOptions(
    format={"pretty"},
    features= "src/features/",
    glue = "cucumber.steps")
 public class cucumberRunner {}

答案 1 :(得分:0)

它与罐子有关。在您的 pom.xml 文件中使用以下依赖项。 1.2.5 有效! 对于其他版本,我遇到了同样的错误,但是当我添加此依赖项时,它对我有用,没有任何错误!

<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>

在您的 pom.xml 文件中添加该依赖项后,将以下几行导入您的代码 -import org.junit.runner.RunWith; -导入黄瓜.api.junit.Cucumber;

答案 2 :(得分:-1)

尝试导入以下内容,

import io.cucumber.junit.CucumberOptions;
import io.cucumber.junit.Cucumber;