3

Before I start, I have asked a similar question here and the answer (Changing 'Gherkin' version) fixed it for me that time. But this time I'm using pom.xml instead of jars and with the newer versions I'm not able to rectify the error. Also, I'm asking this question to narrow down and understand the root cause and prevent similar errors in future.

JUnit Code:

package TestRunner;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)                
@CucumberOptions(features="Features/S1Test.Feature",glue={"StepDefinition"})                        
public class Runner                 
{       

}

pom.xml

<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>S1</groupId>
 <artifactId>S1</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>Load Testing</name>
 <description>Selenium Load Testing Example Using TestNG and Maven</description>

 <!-- Add Following Lines in Your POM File -->
 <properties>
  <selenium.version>2.53.1</selenium.version>
  <testng.version>6.9.10</testng.version>
 </properties>

 <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-core</artifactId>
            <version>4.2.6</version>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-html</artifactId>
            <version>0.2.7</version>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.cobertura</groupId>
            <artifactId>cobertura</artifactId>
            <version>2.1.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>4.2.6</version>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>4.2.6</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-jvm-deps</artifactId>
            <version>1.0.6</version>
            <!-- <scope>provided</scope> -->
        </dependency>
        <dependency>
            <groupId>net.masterthought</groupId>
            <artifactId>cucumber-reporting</artifactId>
            <version>4.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>2.21.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>gherkin</artifactId>
            <version>6.0.17</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.sun</groupId>
            <artifactId>tools</artifactId>
            <version>1.6</version>
            <scope>system</scope>
            <systemPath>C:\Program Files\Java\jdk1.8.0_111\lib\tools.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
            <version>2.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.4.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Exception

java.lang.NoClassDefFoundError: gherkin/IGherkinDialectProvider
    at cucumber.runtime.RuntimeOptionsFactory.create(RuntimeOptionsFactory.java:23)
    at cucumber.api.junit.Cucumber.<init>(Cucumber.java:83)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    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 org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:87)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:73)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:46)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:523)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:761)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:461)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:207)
Caused by: java.lang.ClassNotFoundException: gherkin.IGherkinDialectProvider
    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

Please help me understand why I'm receiving this exception and how I can avoid such errors in the future with updated versions.

justcurious
  • 839
  • 3
  • 12
  • 29

3 Answers3

5

You've added transitive dependencies (dependencies of your dependencies) to your pom. These should be the same version as the ones required by your dependencies. Otherwise you'll see problems like the ones you see now.

So you should remove: cucumber-jvm-deps, cucumber-core and gherkin. Maven will ensure they're present and have the correct version.

You can inspect your dependency tree by using mvn dependency:tree -Dverbose. See Apache Maven Dependency Plugin - Resolving conflicts using the dependency tree .

M.P. Korstanje
  • 10,426
  • 3
  • 36
  • 58
2

Remove or comment-out

<dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>gherkin</artifactId>
        <version>6.0.17</version>
    </dependency>

from your pom file. It will work.

Sourav Das
  • 21
  • 1
2

There are two types of Maven dependencies:

  • Direct: These are dependencies defined in your pom.xml file under the <dependencies/> section.

  • Transitive: These are dependencies that are dependencies of your direct dependencies.

Key Point: We shall not mix direct & transitive dependencies specially their versions! Doing so can cause unpredictable outcome.

Solution: Please remove cucumber-core, cucumber-java, cucumber-jvm-deps, gherkin and Junit. They're transitive dependencies and will be provided by your dependencies.

You can add below set of cucumber minimal dependencies.

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>4.2.6</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-picocontainer</artifactId>
    <version>4.2.6</version>
    <scope>test</scope>
</dependency>

To understand more about this, you may please launch IDE lets say eclipse and open pom.xml -> Go To TAB, Dependencies Hierarchy. Now explore and learn !!

TheSociety
  • 1,936
  • 2
  • 8
  • 20