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

Cucumber Test Runner类运行异常求助:NoClassDefFoundError问题

Fixing java.lang.NoClassDefFoundError & Empty Data Provider in Cucumber + TestNG Non-Maven Project

Problem Context

When running an API automation project built with Rest Assured, Cucumber, and TestNG (non-Maven setup), two critical issues occur:

  1. Runtime error: java.lang.NoClassDefFoundError: io/cucumber/plugin/event/EventPublisher
  2. Test methods are skipped with the message "data provider returns empty array"

Environment Details

  • Used Jars:
    • Cucumber plugin 231.8109.90
    • Cucumber core 7.11.2
    • Cucumber java 7.11.2
    • Cucumber TestNG 7.11.2

Root Cause

  1. Incorrect Dependency: The Cucumber plugin 231.8109.90 is an IntelliJ IDE plugin version, not a valid runtime dependency. This jar is incompatible with your Cucumber core version (7.11.2), leading to missing class definitions like EventPublisher.
  2. Missing Transitive Dependencies: Non-Maven projects require manual inclusion of all dependencies Cucumber relies on (e.g., gherkin, cucumber expressions). These are automatically handled by Maven but must be added manually here.
  3. Empty Data Provider: This is a secondary issue caused by the class-not-found error—Cucumber cannot properly scan feature files or initialize test runners, resulting in an empty dataset for TestNG.

Step-by-Step Solutions

1. Replace the Invalid Cucumber Plugin Jar

Remove the Cucumber plugin 231.8109.90 jar from your project. Replace it with the official cucumber-plugin jar matching your Cucumber core version (7.11.2).

2. Standardize Cucumber Jar Versions

Ensure all Cucumber-related jars are on version 7.11.2:

  • cucumber-core-7.11.2.jar
  • cucumber-java-7.11.2.jar
  • cucumber-testng-7.11.2.jar
  • cucumber-plugin-7.11.2.jar

3. Add Required Transitive Dependencies

Cucumber 7.x depends on several additional jars. Add these to your project classpath:

  • gherkin-28.0.0.jar (compatible with 7.11.2)
  • cucumber-expressions-16.1.0.jar
  • cucumber-gherkin-7.11.2.jar
  • cucumber-tag-expressions-4.1.0.jar
  • testng-7.8.0.jar (or latest compatible version)

4. Correct TestRunner Configuration

Validate your TestRunner class has proper annotations and paths:

import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;

@CucumberOptions(
    features = "src/test/resources/features", // Path to your feature files
    glue = "com.yourpackage.stepdefinitions", // Path to step definition classes
    plugin = {"pretty", "html:target/cucumber-reports.html"}
)
public class TestRunner extends AbstractTestNGCucumberTests {
}
  • Confirm the features path points to your actual feature file directory.
  • Ensure the glue package matches your step definitions' package structure.

5. Verify Classpath Setup

Double-check all jars are added to your project's classpath (in IDE: Project Structure > Modules > Dependencies).

Error Stack Trace Reference

java.lang.NoClassDefFoundError: io/cucumber/plugin/event/EventPublisher
    at java.base/java.lang.ClassLoader.defineClass1(Native Method)
    at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
    at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:800)
    at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:698)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:621)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:579)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    // Rest of stack trace...

Final Checks

  • Clean and rebuild your project after updating jars and configuration.
  • Run the TestRunner class again to confirm both errors are resolved.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 10:16:07