java - cucumber - java.lang.NoClassDefFoundError

标签 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/test/java/Annotation/runTest.java
  2. src/test/java/Annotation/annotation.java
  3. src/test/java/Annotation/annotation.feature
  4. pom.xml

src/test/java/Annotation/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/test/java/Annotation/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/test/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

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

最佳答案

尝试下面的代码:

变化:

  1. 删除了 pom 文件中的重复依赖项
  2. @CucumberOptions 用于最新的 cucumber,而不是 Cucumber.options
  3. 更新了 runTest.java 文件中的导入库
  4. 格式选项已弃用,用插件替换。
  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 { }

注释.特征:

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

如果您有任何疑问,请告诉我。

关于java - cucumber - java.lang.NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43256415/

相关文章:

c# - 当前上下文中不存在名称 'ninja'

java - JSONObject无法通过方法调用转换转换为String

java - 解析后除草

java - Wildfly Swarm (Thorntail) 无法启动 - InvocationTargetException

java - 为什么 Maven 在 target 和 groupId 目录中为相同的源创建两个 jar

javascript - 当我尝试创建提交时出现错误 "Cannot find module ' eslint-config-strict/es 5'"

java - GridBagLayout 不应用网格更改

java - 在另一个类中访问一个类的变量

java - 升级到Struts 2.3.32后出现404错误

java - 如何在 Java 中使用 Cucumber DataTable 匹配包含 ("${") 的参数值