2017-04-06 76 views
0

我学习cucmber并运行测试,而我面临着以下错误:黄瓜 - java.lang.NoClassDefFoundError

java.lang.NoClassDefFoundError: cucumber/io/ResourceLoader 

    at java.lang.Class.getDeclaredConstructors0(Native Method) 
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671) 
    at java.lang.Class.getConstructor0(Class.java:3075) 
    at java.lang.Class.getConstructor(Class.java:1825) 
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104) 
    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.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59) 
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33) 
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49) 
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51) 
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237) 
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 
Caused by: java.lang.ClassNotFoundException: cucumber.io.ResourceLoader 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
    ... 19 more 

文件:

  1. 的src /测试/ JAVA /注解/runTest.java
  2. 的src /测试/ JAVA /注解/ annotation.java
  3. 的src /测试/ JAVA /注解/ annotation.feature
  4. 的pom.xml

的src /测试/ JAVA /注解/ runTest.java

package Annotation; 

import org.junit.runner.RunWith; 
import cucumber.junit.Cucumber; 

@RunWith(Cucumber.class) 
@Cucumber.Options(
     format = {"pretty", "html:target/cucumber"}, 
     features = {"src/test/java/annotation/annotation.feature"} 
) 

public class runTest { } 

的src /测试/ JAVA /注解/ annotation.java

package Annotation; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.chrome.ChromeDriver; 

import cucumber.annotation.en.Given; 
import cucumber.annotation.en.Then; 
import cucumber.annotation.en.When; 

public class annotation { 
    WebDriver driver = null; 
    @Given("^I am on Facebook login page$") 

    public void goToFacebook() { 
     driver = new ChromeDriver(); 
     driver.navigate().to("https://www.facebook.com/"); 
    } 

    @When("^I enter username as \"(.*)\"$") 
    public void enterUsername(String arg1) { 
     driver.findElement(By.id("email")).sendKeys(arg1); 
    } 

    @When ("^I enter password as \"(.*)\"$") 
    public void enterPassword(String arg1) { 
     driver.findElement(By.id("pass")).sendKeys(arg1); 
     driver.findElement(By.id("u_0_v")).click(); 
    } 

    @Then("^Login should fail$") 
    public void checkFail() { 
     if(driver.getCurrentUrl().equalsIgnoreCase(
       "https://www.facebook.com/login.php?login_attempt=1&lwv=110")){ 
      System.out.println("Test1 Pass"); 
     } else { 
      System.out.println("Test1 Failed"); 
     } 
     driver.close(); 
    } 

    @Then("^Relogin option should be available$") 
    public void checkRelogin() { 
     if(driver.getCurrentUrl().equalsIgnoreCase(
       "https://www.facebook.com/login.php?login_attempt=1&lwv=110")){ 
      System.out.println("Test2 Pass"); 
     } else { 
      System.out.println("Test2 Failed"); 
     } 
     driver.close(); 
    } 
} 

的src /测试/java/Annotation/annotation.feature

Feature: annotation 
#This is how background can be used to eliminate duplicate steps 

    Background: 
    User navigates to Facebook Given 
    I am on Facebook login page 

#Scenario with AND 
    Scenario: 
    When I enter username as "TOM" 
    And I enter password as "JERRY" 
    Then Login should fail 

#Scenario with BUT 
    Scenario: 
    When I enter username as "TOM" 
    And I enter password as "JERRY" 
    Then Login should fail 
    But Relogin option should be available 

的pom.xml在pom.xml中

<?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>nice.git.sample</groupId> 
    <artifactId>hello-world</artifactId> 
    <version>1.0-SNAPSHOT</version> 

    <dependencies> 

     <dependency> 
      <groupId>org.seleniumhq.selenium</groupId> 
      <artifactId>selenium-java</artifactId> 
      <version>3.0.1</version> 
     </dependency> 
     <dependency> 
      <groupId>info.cukes</groupId> 
      <artifactId>cucumber-java</artifactId> 
      <version>1.2.5</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>info.cukes</groupId> 
      <artifactId>cucumber-core</artifactId> 
      <version>1.2.5</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>info.cukes</groupId> 
      <artifactId>cucumber-junit</artifactId> 
      <version>1.2.5</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>LATEST</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.seleniumhq.selenium</groupId> 
      <artifactId>selenium-api</artifactId> 
      <version>2.47.1</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>info.cukes</groupId> 
      <artifactId>cucumber-junit</artifactId> 
      <version>1.0.2</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>info.cukes</groupId> 
      <artifactId>cucumber-java</artifactId> 
      <version>1.0.2</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>info.cukes</groupId> 
      <artifactId>cucumber-core</artifactId> 
      <version>1.1.8</version> 
     </dependency> 



    </dependencies> 

</project> 
+0

pom.xml的具有4个罐子多个版本。删除旧版本并尝试... – Grasshopper

回答

0

重复的神器。删除旧版本和更新项目

cucumber-core 
0

试试下面的代码:

变化:

  1. 在删除POM文件一式两份的依赖
  2. @CucumberOptions用于最新的黄瓜黄瓜代替.options
  3. 在runTest.java文件中更新了导入库
  4. fo不推荐使用rmat选项,将其替换为插件。
  5. 在功能文件的背景下更新了步骤。 “鉴于我的Facebook登录页面上的”代替“我的Facebook登录页面上的”
  6. 在annotation.java

pom.xml的方法,更新的正则表达式(依赖):

<dependencies> 

    <dependency> 
     <groupId>org.seleniumhq.selenium</groupId> 
     <artifactId>selenium-java</artifactId> 
     <version>3.0.1</version> 
    </dependency> 
    <dependency> 
     <groupId>info.cukes</groupId> 
     <artifactId>cucumber-java</artifactId> 
     <version>1.2.5</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>info.cukes</groupId> 
     <artifactId>cucumber-core</artifactId> 
     <version>1.2.5</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>info.cukes</groupId> 
     <artifactId>cucumber-junit</artifactId> 
     <version>1.2.5</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>LATEST</version> 
     <scope>test</scope> 
    </dependency> 
</dependencies> 

runTest.java

package Annotation; 

import org.junit.runner.RunWith; 
import cucumber.api.CucumberOptions; 
import cucumber.api.junit.Cucumber; 


@RunWith(Cucumber.class) 
@CucumberOptions(
    plugin = {"pretty","html:target/html-report","json:target/cucumber.json"}, 
    features = {"src/test/java/Annotation/annotation.feature"}, 
    glue = {"Annotation"} 
) 

public class runTest { } 

annotation.feature:

Feature: annotation 
#This is how background can be used to eliminate duplicate steps 

Background: 
User navigates to Facebook 
Given I am on Facebook login page 

#Scenario with AND 
Scenario: 
When I enter username as "TOM" 
And I enter password as "JERRY" 
Then Login should fail 

#Scenario with BUT 
Scenario: 
When I enter username as "TOM" 
And I enter password as "JERRY" 
Then Login should fail 
But Relogin option should be available 

注释。java

package Annotation; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.chrome.ChromeDriver; 

import cucumber.api.java.en.Then; 
import cucumber.api.java.en.When; 
import cucumber.api.java.en.Given; 


public class annotation { 

WebDriver driver = null; 

@Given("^I am on Facebook login page$") 
public void goToFacebook() { 
    driver = new ChromeDriver(); 
    driver.navigate().to("https://www.facebook.com/"); 
} 

@When("^I enter username as \"([^\"]*)\"$") 
public void enterUsername(String arg1) { 
    driver.findElement(By.id("email")).sendKeys(arg1); 
} 

@When ("^I enter password as \"([^\"]*)\"$") 
public void enterPassword(String arg1) { 
    driver.findElement(By.id("pass")).sendKeys(arg1); 
    driver.findElement(By.id("u_0_q")).click(); 
} 

@Then("^Login should fail$") 
public void checkFail() { 
    System.out.println(driver.getCurrentUrl()); 
    if(driver.getCurrentUrl().equalsIgnoreCase(
      "https://www.facebook.com/login.php?login_attempt=1&lwv=110")){ 
     System.out.println("Test1 Pass"); 
    } else { 
     System.out.println("Test1 Failed"); 
    } 
    driver.close(); 
} 

@Then("^Relogin option should be available$") 
public void checkRelogin() { 
    System.out.println(driver.getCurrentUrl()); 
    if(driver.getCurrentUrl().equalsIgnoreCase(
      "https://www.facebook.com/login.php?login_attempt=1&lwv=110")){ 
     System.out.println("Test2 Pass"); 
    } else { 
     System.out.println("Test2 Failed"); 
    } 
    driver.close(); 
    } 
    } 

让我知道你是否有任何疑问。

0

@RunWith(Cucumber.class)

@CucumberOptions(dryRun=true, 

plugin = {"pretty","html:target/html-report","json:target/cucumber.json"}, 

features = {"src/test/java/Annotation/annotation.feature"}, 

glue = {"Annotation"} 
+0

欢迎来到堆栈溢出!您可以通过添加一些解释而不是仅仅发布代码来改善您的答案。 – Ben