在Eclipse运行Cucumber脚本时遇NoSuchMethodError错误求助
java.lang.NoSuchMethodError: cucumber.runtime.RuntimeOptions.<init>(Ljava/util/Properties;[Ljava/lang/String;)V Hey, I’ve run into this exact Cucumber initialization error in Eclipse countless times—almost always it boils down to mismatched dependency versions causing method signature conflicts. Let’s walk through the fixes step by step:
Ensure all Cucumber dependencies use the same version
This is the #1 culprit. Components likecucumber-java,cucumber-junit,cucumber-core, andcucumber-htmlmust share the exact same version number. Mixing major/minor versions (e.g., using Cucumber 5.x for some and 6.x for others) breaks internal method contracts like theRuntimeOptionsconstructor mentioned in your error.For Maven projects, update your
pom.xmlto lock in a consistent version (example using Cucumber 6.11.0):<dependencies> <!-- Cucumber core dependencies --> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-java</artifactId> <version>6.11.0</version> <scope>test</scope> </dependency> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-junit</artifactId> <version>6.11.0</version> <scope>test</scope> </dependency> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-core</artifactId> <version>6.11.0</version> <scope>test</scope> </dependency> <!-- JUnit dependency (match with Cucumber version) --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> </dependencies>For Gradle projects, do the same in
build.gradle:testImplementation 'io.cucumber:cucumber-java:6.11.0' testImplementation 'io.cucumber:cucumber-junit:6.11.0' testImplementation 'junit:junit:4.13.2'Verify JUnit and Cucumber compatibility
Cucumber versions have strict JUnit requirements:- Cucumber 6.x+ requires JUnit 4.13.1 or higher.
- If you’re using JUnit 5, you’ll need to replace
cucumber-junitwithcucumber-junit-platform-engineand add the JUnit 5 platform dependencies.
Avoid mixing JUnit 4 and JUnit 5 dependencies in the same project—this often causes hidden conflicts.
Clean and refresh your Eclipse project
Eclipse sometimes caches outdated dependency files, even after you update your build config. Fix this by:- Right-click your project → Maven → Update Project (check "Force Update of Snapshots/Releases" if using Maven).
- Right-click your project → Clean → Clean Project.
- Restart Eclipse and re-run your
TestRunnerclass.
Check your TestRunner class annotations
Make sure you’re using the correct package for Cucumber annotations—old and new Cucumber versions use different package paths:- For Cucumber 4.x and earlier:
cucumber.api.junit.Cucumber - For Cucumber 5.x and later:
io.cucumber.junit.Cucumber
Example of a correct TestRunner for modern Cucumber:
import org.junit.runner.RunWith; import io.cucumber.junit.Cucumber; import io.cucumber.junit.CucumberOptions; @RunWith(Cucumber.class) @CucumberOptions( features = "src/test/resources/features", glue = "com.yourpackage.stepdefinitions" ) public class TestRunner { }- For Cucumber 4.x and earlier:
Diagnose hidden dependency conflicts
Use your build tool’s dependency tree command to find conflicting versions:- For Maven: Run
mvn dependency:treein your project root. Look for multiple versions ofcucumber-coreor other Cucumber components. - For Gradle: Run
./gradlew dependencies.
If you find a conflicting dependency (e.g., a third-party library pulling in an older Cucumber version), exclude it from your build config. Example for Maven:
<dependency> <groupId>com.some.library</groupId> <artifactId>problematic-library</artifactId> <version>1.0.0</version> <exclusions> <exclusion> <groupId>io.cucumber</groupId> <artifactId>cucumber-core</artifactId> </exclusion> </exclusions> </dependency>- For Maven: Run
内容的提问来源于stack exchange,提问作者Lakshman

