线程“main"中的异常java.lang.NoClassDefFoundError: gherkin/formatter/Formatter [英] Exception in thread "main" java.lang.NoClassDefFoundError: gherkin/formatter/Formatter

查看:30
本文介绍了线程“main"中的异常java.lang.NoClassDefFoundError: gherkin/formatter/Formatter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am learning how to write BDD test scripts in JAVA using Cucumber. However, I keep getting the above error and not sure why. I have the Cukes Gherkin as a dependency.

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: Letter


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

步骤

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);
    }
}

输出

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

非常感谢任何帮助!

推荐答案

我今天正在使用 cucumber 和一些 selenium 脚本,每当我使用 gherkin3 jar 文件时遇到了类似的问题.

I was working with cucumber with some selenium scripts today and came across a similar issue whenever I was using gherkin3 jar file.

一旦我切换回使用 gherkin 2.12.2,问题就消失了.

Once I switch back to using gherkin 2.12.2, the issue went away.

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

You can download the jar from the following location:

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

当然值得一试,看看你是否遇到同样的问题.

It is certainly worth trying this and checking if you get the same issue.

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

I would also try running your feature file without any methods to check if you get it to return the methods you need to create, similar to the what is detailed in the document here:

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

当您只需要运行功能文件时,您不需要粘合选项,尽管如示例中详述的那样.

You don't need the glue option though like detailed in the example when you just what to run the feature file.

这篇关于线程“main"中的异常java.lang.NoClassDefFoundError: gherkin/formatter/Formatter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆