You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cucumber测试无法运行:NoClassDefFoundError错误修复咨询

修复Cucumber初始化错误:java.lang.NoClassDefFoundError: io/cucumber/core/options/CucumberOptionsAnnotationParser$OptionsProvider

问题根源

你的项目同时混用了旧版Cucumber(groupId为info.cukes,版本1.2.6)和新版Cucumber(groupId为io.cucumber,版本7.10.0),两者的API结构完全不兼容,导致类加载冲突触发错误。同时测试类和步骤定义类的导入包也存在新旧混用的情况,进一步加剧了问题。


修复步骤

1. 清理pom.xml,统一使用新版Cucumber依赖

删除所有info.cukes开头的依赖,以及多余的cucumber-jvm-deps,只保留兼容的新版依赖(适配你用的JUnit 4):

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.alchemy.learning</groupId>
   <artifactId>CucumberLearning</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <properties>
      <maven.compiler.source>17</maven.compiler.source>
      <maven.compiler.target>17</maven.compiler.target>
      <!-- 统一Cucumber版本,方便后续维护 -->
      <cucumber.version>7.10.0</cucumber.version>
   </properties>

   <dependencies>
      <!-- Cucumber核心依赖 -->
      <dependency>
         <groupId>io.cucumber</groupId>
         <artifactId>cucumber-java</artifactId>
         <version>${cucumber.version}</version>
         <scope>test</scope>
      </dependency>
      <!-- Cucumber JUnit 4适配器 -->
      <dependency>
         <groupId>io.cucumber</groupId>
         <artifactId>cucumber-junit</artifactId>
         <version>${cucumber.version}</version>
         <scope>test</scope>
      </dependency>
      <!-- JUnit 4依赖 -->
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.13.2</version>
         <scope>test</scope>
      </dependency>
   </dependencies>
</project>

2. 更新步骤定义类(StepDef.java)的导入包

新版Cucumber的API包路径是io.cucumber.java,替换旧版的cucumber.api.java:

package stepdefinitions;

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

public class StepDef {
 
 @Given("User on webapp")
 public void user_on_webapp() throws Throwable {
  System.out.print("Webapp is open");
 }

 @When("I enter username as \"(.*)\" and password as \"(.*)\"")
 public void i_enter_username_as_and_password_as(String arg1, String arg2) throws Throwable {
  System.out.print("Enter the username and password in the fields");
 }
 
 @When("I submit login page")
 public void i_submit_login_page() throws Throwable {
  System.out.print("Clicked on submit button");
 }

 @Then("I redirect to UserHome page")
 public void i_redirect_to_user_home_page() throws Throwable {
  System.out.print("Successfully logged in");
 }
 
}

注:可以去掉正则表达式中的^和$,Cucumber会自动匹配整行文本;如果需要精确匹配也可以保留,新版完全兼容。

3. 修正测试运行类(Registration.java)的导入包

新版Cucumber的CucumberOptions注解在io.cucumber.junit包下,确保所有导入统一:

package stepdefinitions;

import org.junit.runner.RunWith;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(features="classpath:test.feature", glue = "stepdefinitions", plugin = {"html:target/test-report"})
public class Registration {
  
}

4. 优化feature文件(可选)

你的Scenario Outline里写死了参数值,应该用Examples中的变量替换,否则Outline失去意义:

Feature: User Registration

  @web
  Scenario Outline: Validate Registration Field
    Given User on webapp
    When I enter username as "<uname>" and password as "<pass>"
    And I submit login page
    Then I redirect to UserHome page

    Examples: 
      | uname  | pass     | 
      | Mia    | Mia1234  | 
      | admin  | admin    | 

最后执行步骤

  1. 执行mvn clean test命令清理旧依赖并重新构建项目
  2. 运行Registration.java作为JUnit测试

这样就能解决类找不到的初始化错误,顺利执行Cucumber测试。

内容的提问来源于stack exchange,提问作者user20148665

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 13:10:45