线程“main”中的异常 java.lang.NoClassDefFoundError: gherkin/formatter/Formatter

2024-02-27

我正在学习如何使用 Cucumber 在 JAVA 中编写 BDD 测试脚本。但是,我不断收到上述错误,但不知道为什么。我有 Cukes Gherkin 作为依赖。

POM

<?xml version="1.0" encoding="UTF-8"?>
<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>Cucumber</groupId>
    <artifactId>Cucumber</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>gherkin</artifactId>
            <version>1.2.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>1.2.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-jvm-deps</artifactId>
            <version>1.0.5</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>com.thoughtworks.xstream</groupId>
                    <artifactId>xstream</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.googlecode.java-diff-utils</groupId>
                    <artifactId>diffutils</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-core</artifactId>
            <version>1.2.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>1.2.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-picocontainer</artifactId>
            <version>1.2.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.picocontainer</groupId>
            <artifactId>picocontainer</artifactId>
            <version>2.15</version>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>codehaus</id>
            <url>http://repository.codehaus.org</url>
        </repository>
    </repositories>

    <profiles>
        <profile>
            <id>junit-4.12</id>
            <properties>
                <junit.version>4.12</junit.version>
            </properties>
        </profile>

    </profiles>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.5</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-clean-plugin</artifactId>
                <version>2.6.1</version>
                <configuration>
                    <filesets>
                        <fileset>
                            <directory>.</directory>
                            <includes>
                                <include>**/*.ser</include>
                            </includes>
                        </fileset>
                    </filesets>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Feature

Feature: Letter


    Scenario: Check Letter
    Given I have the letter "A"
      When Icheck the letter "A"
      Then I should see an output

Steps

package cucumber.steps;
import cucumber.api.CucumberOptions;
import cucumber.api.java.en.*;
import cucumber.api.junit.Cucumber;
import org.junit.Assert;
import org.junit.runner.RunWith;

/**
 * Created by Dustin on 8/31/2015.
 */

@RunWith(Cucumber.class)
@CucumberOptions(
        plugin = {"json-pretty", "html:target/cucumber"},
        features = "src/main/java/cucumber/steps/LetterStepDefs"
)

public class LetterStepDefs {

    private String letter;
    private String message;

    @Given("^I have the letter \"([^\"]*)\"$")
    public void I_have_the_letter(String letter) throws Throwable {
        // Express the Regexp above with the code you wish you had
        this.letter = letter;
    }

    @When("^Icheck the letter \"([^\"]*)\"$")
    public void Icheck_the_letter(String letter) throws Throwable {
        // Express the Regexp above with the code you wish you had
        try
        {
            Assert.assertEquals(this.letter, letter);
        }
        catch (Exception exc)
        {
            message = exc.getMessage();
        }
    }

    @Then("^I should see an output$")
    public void I_should_see_an_output() throws Throwable {
        // Express the Regexp above with the code you wish you had
        System.out.println(message);
    }
}

Output

Testing started at 4:41 PM ...
Connected to the target VM, address: '127.0.0.1:58473', transport: 'socket'
JUnit version 4.12
Exception in thread "main" java.lang.NoClassDefFoundError: gherkin/formatter/Formatter
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:274)
    at org.junit.internal.Classes.getClass(Classes.java:16)
    at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:100)
    at org.junit.runner.JUnitCommandLineParseResult.parseArgs(JUnitCommandLineParseResult.java:50)
    at org.junit.runner.JUnitCommandLineParseResult.parse(JUnitCommandLineParseResult.java:44)
    at org.junit.runner.JUnitCore.runMain(JUnitCore.java:72)
    at org.junit.runner.JUnitCore.main(JUnitCore.java:36)
Caused by: java.lang.ClassNotFoundException: gherkin.formatter.Formatter
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 20 more
Disconnected from the target VM, address: '127.0.0.1:58473', transport: 'socket'

Process finished with exit code 1

任何帮助深表感谢!


今天我正在使用一些硒脚本处理黄瓜,每当我使用 gherkin3 jar 文件时都会遇到类似的问题。

当我切换回使用 Gherkin 2.12.2 后,问题就消失了。

您可以从以下位置下载该 jar:

http://search.maven.org/#search%7Cga%7C1%7Cgherkin http://search.maven.org/#search%7Cga%7C1%7Cgherkin

这当然值得尝试并检查您是否遇到同样的问题。

我还会尝试在不使用任何方法的情况下运行您的功能文件,以检查它是否返回您需要创建的方法,类似于此处文档中详细介绍的内容:

http://www.toolsqa.com/cucumber/first-cucumber-selenium-java-test/ http://www.toolsqa.com/cucumber/first-cucumber-selenium-java-test/

当您只需要运行功能文件时,您不需要像示例中详细说明的那样使用粘合选项。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

线程“main”中的异常 java.lang.NoClassDefFoundError: gherkin/formatter/Formatter 的相关文章

随机推荐