Cucumber - java.lang.NoClassDefFoundError

时间:2017-04-06 13:18:51

标签: java maven cucumber

我正在学习cucmber,我在运行测试时遇到以下错误:

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
  5. 的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.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

    <?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>
    

3 个答案:

答案 0 :(得分:1)

尝试以下代码:

的变化:

  1. 删除了pom文件中的重复依赖项
  2. @CucumberOptions用于最新的黄瓜而不是Cucumber.options
  3. 更新了runTest.java文件中的导入库
  4. 不推荐使用
  5. format选项,将其替换为插件。
  6. 更新了要素文件中背景下的步骤。 “鉴于我在Facebook登录页面”取代“我在Facebook登录页面”
  7. 更新了annotation.java
  8. 中方法的正则表达式

    的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
    

    annotation.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();
       }
      }
    

    如果您有任何疑问,请与我们联系。

答案 1 :(得分:0)

pom.xml中的重复工件。删除旧版本并更新项目

cucumber-core

答案 2 :(得分: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"}