黄瓜异常:java.lang.ClassNotFoundException:cucumber.io.ResourceLoader

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25291480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:03:03  来源:igfitidea点击:

Cucumber Exception: java.lang.ClassNotFoundException: cucumber.io.ResourceLoader

javajunitcucumbercucumber-jvmcucumber-junit

提问by Zain

I am facing an error whenever I run the cucumber test by Junit. Here is the following Exception. Any help will be awesome

每当我通过 Junit 运行黄瓜测试时,我都会遇到错误。这是以下异常。任何帮助都会很棒

NOTE:

笔记:

THERE IS NO ERROR IN THE PROJECT

项目没有错误

java.lang.NoClassDefFoundError: cucumber/io/ResourceLoader
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2398)
    at java.lang.Class.getConstructor0(Class.java:2708)
    at java.lang.Class.getConstructor(Class.java:1659)
    at cucumber.runtime.Reflections.hasConstructor(Reflections.java:53)
    at cucumber.runtime.Reflections.instantiateSubclasses(Reflections.java:29)
    at cucumber.runtime.Runtime.loadBackends(Runtime.java:96)
    at cucumber.runtime.Runtime.<init>(Runtime.java:63)
    at cucumber.api.junit.Cucumber.createRuntime(Cucumber.java:74)
    at cucumber.api.junit.Cucumber.<init>(Cucumber.java:61)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    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.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: cucumber.io.ResourceLoader
    at java.net.URLClassLoader.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    ... 28 more

Here is how my POM.xml looks like:

这是我的 POM.xml 的样子:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>Test</groupId>
  <artifactId>Cucumber</artifactId>
  <version>0.0.1-SNAPSHOT</version>
<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>

           <!-- Cucumber -->   
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>1.1.8</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-chrome-driver</artifactId>
            <version>2.42.2</version>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>1.0.0.RC5</version>
            <scope>test</scope>
        </dependency>
</dependencies>
</project>

Here is the image of my project structure: enter image description here

这是我的项目结构的图像: 在此处输入图片说明

And here is my RunIT.Java Class

这是我的 RunIT.Java 类

package com.Cucumber;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;


@RunWith(Cucumber.class)
@CucumberOptions(
        tags={"@mysingle"},
        features = {"."})
public class RunIT {

}

采纳答案by Matt Watson

It looks like you're missing the cucumber-core library.

看起来您缺少黄瓜核心库。

Try adding the following to your dependencies.

尝试将以下内容添加到您的依赖项中。

    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-core</artifactId>
        <version>1.1.8</version>
    </dependency>

Also, you appear to be mixing and matching versions. Try using the same version for all the cucumber-* libraries.

此外,您似乎正在混合和匹配版本。尝试对所有 Cucumber-* 库使用相同的版本。

回答by Star Wing

You are trying to run Cucumber in java, and you haven't loaded the Cucumber-java dependency? How's your Cucumber supposed to execute?

您正在尝试在 Java 中运行 Cucumber,并且您还没有加载 Cucumber-java 依赖项?你的 Cucumber 应该如何执行?

Here are the minimum dependencies needed to get your Cucumber up & running:

以下是启动和运行 Cucumber 所需的最低依赖项:

-Cucumber-java (1.2.2) -Cucumber-junit (1.2.2) -Cucumber-picocontainer (1.2.2) -junit (4.12)

-Cucumber-java (1.2.2) -Cucumber-junit (1.2.2) -Cucumber-picocontainer (1.2.2) -junit (4.12)

Maven manages the rest of the dependencies. Make sure you have the latest dependencies, and that you are not mixing and matching versions (for example, I've used all 1.2.2 version)

Maven 管理其余的依赖项。确保您拥有最新的依赖项,并且您没有混合和匹配版本(例如,我使用了所有 1.2.2 版本)

Edit: Just noticed that your Cucumber-java is at the bottom of the pom.xml. It's a good practice to order your dependencies....all cucumber dependencies go together, one after another.

编辑:刚刚注意到您的 Cucumber-java 位于 pom.xml 的底部。对依赖项进行排序是一种很好的做法……所有黄瓜依赖项一个接一个地放在一起。

回答by Abhishesh

Make sure all your cucumber-* jars have the same version, Try this thing - It should resolve your issue.

确保您所有的黄瓜-* 罐子都有相同的版本,试试这个 - 它应该可以解决您的问题。