我正在学习 Cucumber,但遇到了 FeatureParser 错误
java.lang.NoClassDefFoundError: io/cucumber/core/gherkin/FeatureParser
我导入了以下jar文件
我写了如下代码
LoginTestRunner.java
package cucumberTests;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions(features="features", glue="stepImplementations")
public class LoginTestRunner {
}
我也写了功能文件。
出现以下错误:
java.lang.NoClassDefFoundError: io/cucumber/core/gherkin/FeatureParser
at io.cucumber.core.feature.FeatureParser.parseResource(FeatureParser.java:35)
at io.cucumber.core.runtime.FeaturePathFeatureSupplier.lambda$new$0(FeaturePathFeatureSupplier.java:39)
at java.base/java.util.function.BiFunction.lambda$andThen$0(BiFunction.java:70)
at io.cucumber.core.resource.ResourceScanner.lambda$processResource$2(ResourceScanner.java:126)
at io.cucumber.core.resource.PathScanner$ResourceFileVisitor.visitFile(PathScanner.java:67)
at io.cucumber.core.resource.PathScanner$ResourceFileVisitor.visitFile(PathScanner.java:52)
at java.base/java.nio.file.Files.walkFileTree(Files.java:2724)
at java.base/java.nio.file.Files.walkFileTree(Files.java:2796)
at io.cucumber.core.resource.PathScanner.findResourcesForPath(PathScanner.java:46)
at io.cucumber.core.resource.PathScanner.findResourcesForUri(PathScanner.java:26)
at io.cucumber.core.resource.ResourceScanner.findResourcesForUri(ResourceScanner.java:109)
at io.cucumber.core.resource.ResourceScanner.scanForResourcesUri(ResourceScanner.java:86)
at io.cucumber.core.runtime.FeaturePathFeatureSupplier.loadFeatures(FeaturePathFeatureSupplier.java:62)
at io.cucumber.core.runtime.FeaturePathFeatureSupplier.get(FeaturePathFeatureSupplier.java:46)
at io.cucumber.junit.Cucumber.<init>(Cucumber.java:156)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.vintage.engine.discovery.DefensiveAllDefaultPossibilitiesBuilder$DefensiveAnnotatedBuilder.buildRunner(DefensiveAllDefaultPossibilitiesBuilder.java:113)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.vintage.engine.discovery.DefensiveAllDefaultPossibilitiesBuilder.runnerForClass(DefensiveAllDefaultPossibilitiesBuilder.java:56)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.vintage.engine.discovery.ClassSelectorResolver.resolveTestClass(ClassSelectorResolver.java:66)
at org.junit.vintage.engine.discovery.ClassSelectorResolver.resolve(ClassSelectorResolver.java:47)
at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.lambda$resolve$2(EngineDiscoveryRequestResolution.java:129)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
at java.base/java.util.ArrayList$ArrayListSpliterator.tryAdvance(ArrayList.java:1631)
at java.base/java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:127)
at java.base/java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:502)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:488)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:150)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:543)
at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.resolve(EngineDiscoveryRequestResolution.java:174)
at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.resolve(EngineDiscoveryRequestResolution.java:120)
at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.resolveCompletely(EngineDiscoveryRequestResolution.java:87)
at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.run(EngineDiscoveryRequestResolution.java:80)
at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolver.resolve(EngineDiscoveryRequestResolver.java:112)
at org.junit.vintage.engine.discovery.VintageDiscoverer.discover(VintageDiscoverer.java:42)
at org.junit.vintage.engine.VintageTestEngine.discover(VintageTestEngine.java:62)
at org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:177)
at org.junit.platform.launcher.core.DefaultLauncher.discoverRoot(DefaultLauncher.java:164)
at org.junit.platform.launcher.core.DefaultLauncher.discover(DefaultLauncher.java:120)
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.<init>(JUnit5TestReference.java:45)
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.createUnfilteredTest(JUnit5TestLoader.java:76)
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.createTest(JUnit5TestLoader.java:66)
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.loadTests(JUnit5TestLoader.java:53)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:526)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
Caused by: java.lang.ClassNotFoundException: io.cucumber.core.gherkin.FeatureParser
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 56 more
我正在学习 Cucumber,但遇到了 FeatureParser 错误
如果您遵循 10 分钟教程,您将获得使用 Maven 依赖项管理的介绍。
除了本教程之外,我强烈建议您花时间学习 Maven 或 Gradle 以及 Cucumber。除此之外,这些工具将自动化您的依赖管理,这可以让您的生活变得更加轻松。
例如:
如果您想将 Cucumber 与 JUnit 4 和基于注释的步骤定义一起使用,您可以在 Maven
pom.xml
文件中声明这一最小依赖集。
<properties>
<cucumber.version>5.2.0</cucumber.version>
</properties>
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
通过告诉 Maven 你的依赖是什么,Maven 可以计算你的传递依赖,即:你的依赖的依赖。
这有很多优点。一个示例是使用
mvn dependency:tree -Dverbose=true
命令列出所有依赖项。与手动下载 jar 文件并希望您拥有正确的文件相比,这要快得多且不易出错。
$ mvn dependency:tree -Dverbose=true
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< cucumber:cucumber-java-skeleton >-------------------
[INFO] Building Cucumber-Java Skeleton 0.0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ cucumber-java-skeleton ---
[INFO] cucumber:cucumber-java-skeleton:jar:0.0.1
[INFO] +- io.cucumber:cucumber-java:jar:5.2.0:test
[INFO] | +- io.cucumber:cucumber-core:jar:5.2.0:test
[INFO] | | +- io.cucumber:cucumber-gherkin:jar:5.2.0:test
[INFO] | | +- io.cucumber:cucumber-gherkin-vintage:jar:5.2.0:test
[INFO] | | +- io.cucumber:tag-expressions:jar:2.0.4:test
[INFO] | | +- io.cucumber:cucumber-expressions:jar:8.3.1:test
[INFO] | | +- io.cucumber:datatable:jar:3.3.0:test
[INFO] | | +- io.cucumber:cucumber-plugin:jar:5.2.0:test
[INFO] | | \- io.cucumber:docstring:jar:5.2.0:test
[INFO] | \- org.apiguardian:apiguardian-api:jar:1.1.0:test
[INFO] +- io.cucumber:cucumber-junit:jar:5.2.0:test
[INFO] \- junit:junit:jar:4.13:test
[INFO] \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.781 s
[INFO] Finished at: 2020-02-10T23:00:14+01:00
[INFO] ------------------------------------------------------------------------
添加以下依赖项并重建您的项目:
注意范围设置为测试--
当在编译时找到该类时会抛出java.lang.NoClassDefFoundError
,但在运行时则不会。所以你需要确保这个 .jar 在运行时可用
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>5.1.3</version>
<scope>test</scope>
</dependency>
如果您使用的是 Eclipse,请确保项目类路径设置为包含依赖项,并且它已构建并嵌入到项目中
在您的 testrunner.java 文件中,尝试给出功能文件的完整路径
当 Cucumber 框架找不到应该在运行时可用的类时,会发生 java.lang.NoClassDefFoundError: io/cucumber/core/gherkin/FeatureParser 错误。这通常表明您的 Cucumber 依赖项设置存在问题,例如 Cucumber 或 Gherkin 库版本缺失或不兼容。
解决方法如下:
确保您的 pom.xml(如果使用 Maven)或 build.gradle(如果使用 Gradle)中有正确版本的 Cucumber 和 Gherkin。 FeatureParser 类是 Gherkin 模块的一部分,因此您还需要包含正确的 Gherkin 依赖项。
如果您使用 Maven,请确保您的 pom.xml 中包含以下依赖项:
确保 Cucumber 和 Gherkin 版本兼容。有时,出现NoClassDefFoundError是因为不同版本的库不兼容。例如,如果您使用的是 Cucumber 7.x,请确保您使用的是相应版本的 Gherkin,约为 27.x 左右。 3.清理并重建您的项目
更新依赖项后,清理并重建项目,以确保正确下载并链接所有必需的库:
如果您使用的其他库可能依赖于 Gherkin,请确保它们不会引入冲突版本的 Gherkin 库。您可以使用以下方法检查依赖项冲突:
通过排除旧版本或强制使用所需版本来解决任何版本冲突。 5.更新Cucumber插件(如果适用)
如果您正在使用任何 Cucumber 插件(例如,对于 IntelliJ 或 Eclipse 等 IDE),请确保将它们更新到与您正在使用的 Cucumber 版本匹配的最新版本。 6. 重新检查类路径设置
确保所有必需的 JAR(包括 Gherkin 库)在您的类路径上可用。如果从 IDE 运行,请验证所有必需的依赖项是否已正确链接到您的项目。