Stav Alfi
Stav Alfi

Reputation: 13953

Cucumber - java.lang.NoClassDefFoundError

I'm learning cucmber and I'm facing the following errors while running the test:

java.lang.NoClassDefFoundError: cucumber/io/ResourceLoader

    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
    at java.lang.Class.getConstructor0(Class.java:3075)
    at java.lang.Class.getConstructor(Class.java:1825)
    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 com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.ClassNotFoundException: cucumber.io.ResourceLoader
    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

Files:

  1. src/test/java/Annotation/runTest.java
  2. src/test/java/Annotation/annotation.java
  3. src/test/java/Annotation/annotation.feature
  4. pom.xml

src/test/java/Annotation/runTest.java

package Annotation;

import org.junit.runner.RunWith;
import cucumber.junit.Cucumber;

@RunWith(Cucumber.class)
@Cucumber.Options(
        format = {"pretty", "html:target/cucumber"},
        features = {"src/test/java/annotation/annotation.feature"}
)

public class runTest { }

src/test/java/Annotation/annotation.java

package Annotation;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import cucumber.annotation.en.Given;
import cucumber.annotation.en.Then;
import cucumber.annotation.en.When;

public class annotation {
    WebDriver driver = null;
    @Given("^I am on Facebook login page$")

    public void goToFacebook() {
        driver = new ChromeDriver();
        driver.navigate().to("https://www.facebook.com/");
    }

    @When("^I enter username as \"(.*)\"$")
    public void enterUsername(String arg1) {
        driver.findElement(By.id("email")).sendKeys(arg1);
    }

    @When ("^I enter password as \"(.*)\"$")
    public void enterPassword(String arg1) {
        driver.findElement(By.id("pass")).sendKeys(arg1);
        driver.findElement(By.id("u_0_v")).click();
    }

    @Then("^Login should fail$")
    public void checkFail() {
        if(driver.getCurrentUrl().equalsIgnoreCase(
                "https://www.facebook.com/login.php?login_attempt=1&lwv=110")){
            System.out.println("Test1 Pass");
        } else {
            System.out.println("Test1 Failed");
        }
        driver.close();
    }

    @Then("^Relogin option should be available$")
    public void checkRelogin() {
        if(driver.getCurrentUrl().equalsIgnoreCase(
                "https://www.facebook.com/login.php?login_attempt=1&lwv=110")){
            System.out.println("Test2 Pass");
        } else {
            System.out.println("Test2 Failed");
        }
        driver.close();
    }
}

src/test/java/Annotation/annotation.feature

Feature: annotation
#This is how background can be used to eliminate duplicate steps

  Background:
  User navigates to Facebook Given
  I am on Facebook login page

#Scenario with AND
  Scenario:
    When I enter username as "TOM"
    And I enter password as "JERRY"
    Then Login should fail

#Scenario with BUT
  Scenario:
    When I enter username as "TOM"
    And I enter password as "JERRY"
    Then Login should fail
    But Relogin option should be available

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>nice.git.sample</groupId>
    <artifactId>hello-world</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>1.2.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-core</artifactId>
            <version>1.2.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>1.2.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>LATEST</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-api</artifactId>
            <version>2.47.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>1.0.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>1.0.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-core</artifactId>
            <version>1.1.8</version>
        </dependency>



    </dependencies>

</project>

Upvotes: 0

Views: 13855

Answers (3)

Akarsh
Akarsh

Reputation: 967

Try below code:

Changes:

  1. Removed duplicate dependencies in pom file
  2. @CucumberOptions is used for the latest cucumber instead of Cucumber.options
  3. Updated import libraries in runTest.java file
  4. format option is deprecated, replaced it with plugin.
  5. Updated step under background in feature file. "Given I am on Facebook login page" in place of "I am on Facebook login page"
  6. Updated regular expressions for methods in annotation.java

Pom.xml(dependencies):

<dependencies>

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.0.1</version>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>1.2.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-core</artifactId>
        <version>1.2.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>1.2.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>LATEST</version>
        <scope>test</scope>
    </dependency>
</dependencies>

runTest.java

package Annotation;

import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;


@RunWith(Cucumber.class)
@CucumberOptions(
    plugin = {"pretty","html:target/html-report","json:target/cucumber.json"},
    features = {"src/test/java/Annotation/annotation.feature"},
    glue = {"Annotation"}
)

public class runTest { }

annotation.feature:

Feature: annotation
#This is how background can be used to eliminate duplicate steps

Background:
User navigates to Facebook 
Given I am on Facebook login page

#Scenario with AND
Scenario:
When I enter username as "TOM"
And I enter password as "JERRY"
Then Login should fail

#Scenario with BUT
Scenario:
When I enter username as "TOM"
And I enter password as "JERRY"
Then Login should fail
But Relogin option should be available

annotation.java

package Annotation;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

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


public class annotation {

WebDriver driver = null;

@Given("^I am on Facebook login page$")
public void goToFacebook() {
    driver = new ChromeDriver();
    driver.navigate().to("https://www.facebook.com/");
}

@When("^I enter username as \"([^\"]*)\"$")
public void enterUsername(String arg1) {
    driver.findElement(By.id("email")).sendKeys(arg1);
}

@When ("^I enter password as \"([^\"]*)\"$")
public void enterPassword(String arg1) {
    driver.findElement(By.id("pass")).sendKeys(arg1);
    driver.findElement(By.id("u_0_q")).click();
}

@Then("^Login should fail$")
public void checkFail() {
    System.out.println(driver.getCurrentUrl());
    if(driver.getCurrentUrl().equalsIgnoreCase(
            "https://www.facebook.com/login.php?login_attempt=1&lwv=110")){
        System.out.println("Test1 Pass");
    } else {
        System.out.println("Test1 Failed");
    }
    driver.close();
}

@Then("^Relogin option should be available$")
public void checkRelogin() {
    System.out.println(driver.getCurrentUrl());
    if(driver.getCurrentUrl().equalsIgnoreCase(
            "https://www.facebook.com/login.php?login_attempt=1&lwv=110")){
        System.out.println("Test2 Pass");
    } else {
        System.out.println("Test2 Failed");
    }
    driver.close();
   }
  }

Let me know if you have any queries.

Upvotes: 1

ali
ali

Reputation: 1

@RunWith(Cucumber.class)

@CucumberOptions(dryRun=true,

plugin = {"pretty","html:target/html-report","json:target/cucumber.json"},

features = {"src/test/java/Annotation/annotation.feature"},

glue = {"Annotation"}

Upvotes: 0

Ivan Pronin
Ivan Pronin

Reputation: 1916

Duplicated artifact in pom.xml. Remove the old version and update project

cucumber-core

Upvotes: 0

Related Questions