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:
- Runtime error:
java.lang.NoClassDefFoundError: io/cucumber/plugin/event/EventPublisher - 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
- Incorrect Dependency: The
Cucumber plugin 231.8109.90is 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 likeEventPublisher. - 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.
- 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.jarcucumber-java-7.11.2.jarcucumber-testng-7.11.2.jarcucumber-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.jarcucumber-gherkin-7.11.2.jarcucumber-tag-expressions-4.1.0.jartestng-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
featurespath points to your actual feature file directory. - Ensure the
gluepackage 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
相关产品推荐
相关产品推荐

