2015-08-31 38 views
0

我正在學習如何使用Cucumber在JAVA中編寫BDD測試腳本。但是,我不斷收到上述錯誤,不知道爲什麼。我有Cukes小黃瓜作爲依賴。線程「main」中的異常java.lang.NoClassDefFoundError:gherkin/formatter/Formatter

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 

任何幫助非常感謝!

回答

2

我今天正在用一些硒腳本來處理黃瓜,每當我使用gherkin3 jar文件時遇到類似的問題。

一旦我切換回使用小黃瓜2.12.2,問題就消失了。

您可以從以下位置下載JAR:

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

這當然是值得嘗試這個,如果你得到了同樣的問題檢查。

我也嘗試運行的特徵文件,沒有任何方法來檢查,如果你得到它回報你需要創建,類似什麼是這裏的文件中詳述的方法:

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

您不需要膠水選項,但是您可以在示例中詳細介紹何時運行該功能文件。

相關問題