Sean S B Sean S B - 3 years ago 339
Java Question

Cucumber error with Eclipse

I get a

java.lang.NoClassDefFoundError: gherkin/formatter/Formatter
when I execute my cucumber JUnit test in Eclipse.

My environment:

Eclipse Neon 4.6.3
JUnit 4.12
Cucumber-core 1.2.5
Cucumber-java 1.2.5
Cucumber-junit 1.2.5
Hamcrest-core 1.3


Reference to cucumber in eclipse: eclipse\help\Installed software 
tab=cucumber Eclipse feature ver 0.0.17.201704062133.

Eclipse Environment Setup

enter image description here

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>com.javacodegeeks</groupId>
<artifactId>junitcucumber</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>JUnit Cucumber Example</name>
<description>JUnit Cucumber Example</description>
<dependencies>

<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.5</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>

</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>


UserSteps.java

package junitcucumber;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import junitcucumber.User;

public class UserSteps {

private User user = new User();

@Given("^that the user (.*) is given a task to clear (.*) certification exam$")
public void certificationName(String name, String certication) throws Throwable {
user.setName(name);
user.setCertification(certication);
}

@When("^(.*) got (\\d+) marks in exam$")
public void gotMarks(String name, int marks) throws Throwable {
user.setName(name);
user.setMarks(marks);
}

@Then("^(.*) is known as (.*) certified$")
public void certifiedYes(String name, String certification) throws Throwable {
assertThat(name, is(user.getName()));
assertThat(user.getCertification(), equalTo("Java"));
assertThat(user.getResult(), is(true));
}
}


user.feature

Feature: User Certification

Scenario: User is Passed
Given that the user Vinod is given a task to clear Java certification exam
When Vinod got 60 marks in exam
Then Vinod is known as Java certified


UserTest.java

package junitcucumber;

import org.junit.runner.RunWith;

import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
public class UserTest {

}


User.java

package junitcucumber;

public class User {
private String name;
private String certification;
private int marks;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getCertification() {
return certification;
}

public void setCertification(String certification) {
this.certification = certification;
}

public int getMarks() {
return marks;
}

public void setMarks(int marks) {
this.marks = marks;
}

public boolean getResult() {
if (this.marks < 60) {
return false;
} else {
return true;
}
}
}


Eclipse project class path:
enter image description here

Stack Trace:

java.lang.NoClassDefFoundError: gherkin/formatter/Formatter
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
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)
at cucumber.runtime.formatter.PluginFactory$1.<init>(PluginFactory.java:53)
at cucumber.runtime.formatter.PluginFactory.<clinit>(PluginFactory.java:52)
at cucumber.runtime.RuntimeOptions.<init>(RuntimeOptions.java:70)
at cucumber.runtime.RuntimeOptionsFactory.create(RuntimeOptionsFactory.java:25)
at cucumber.api.junit.Cucumber.<init>(Cucumber.java:56)
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:84)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.ClassNotFoundException: gherkin.formatter.Formatter
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)
... 34 more

Answer Source

The Gherkin JAR is missing in your Java build path (see Referenced Libraries in your screenshot):

Add Gherkin as dependeny to your pom.xml.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download