Search code examples
cucumbercucumber-jvmcucumber-javacucumber-junit

Cucumber tests won't run


I'm trying to use Cucumber to register a user on a web app but every time I run Registration.java as a Junit test, I get the initialisation error "java.lang.NoClassDefFoundError: io/cucumber/core/options/CucumberOptionsAnnotationParser$OptionsProvider". How can I fix this?

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.alchemy.learning</groupId>
   <artifactId>CucumberLearning</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <properties>
      <maven.compiler.source>17</maven.compiler.source>
      <maven.compiler.target>17</maven.compiler.target>
   </properties>

   <dependencies>
      <dependency>
         <groupId>info.cukes</groupId>
         <artifactId>cucumber-jvm</artifactId>
         <version>1.2.6</version>
         <type>pom</type>
      </dependency>
      <dependency>
         <groupId>info.cukes</groupId>
         <artifactId>cucumber-junit</artifactId>
         <version>1.2.6</version>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>info.cukes</groupId>
         <artifactId>cucumber-java</artifactId>
         <version>1.2.6</version>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.13.2</version>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>io.cucumber</groupId>
         <artifactId>cucumber-junit</artifactId>
         <version>7.10.0</version>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>io.cucumber</groupId>
         <artifactId>cucumber-jvm-deps</artifactId>
         <version>1.0.6</version>
      </dependency>
   </dependencies>
</project>

test.feature

Feature: User Registration

  @web
  Scenario Outline: Validate Registration Field
    Given User on webapp
    When I enter username as "Mia" and password as "Mia1234"
    And I submit login page
    Then I redirect to UserHome page

    Examples: 
      | uname  | pass | 
      | Mia |   Mia1234  | 
      | admin |  admin  | 

StepDef.java

package stepdefinitions;

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

public class StepDef {
 
 @Given("^User on webapp$")
 public void user_on_webapp() throws Throwable {
  System.out.print("Webapp is open");
 }

 @When("^I enter username as \"([^\"]*)\" and password as \"([^\"]*)\"$")
 public void i_enter_username_as_and_password_as(String arg1, String arg2) throws Throwable {
  System.out.print("Enter the username and password in the fields");
 }
 
 @When("^I submit login page$")
 public void i_submit_login_page() throws Throwable {
  System.out.print("Clicked on submit button");
 }

 @Then("^I redirect to UserHome page$")
 public void i_redirect_to_user_home_page() throws Throwable {
  System.out.print("Successfully logged in");
 }
 
}

Registration.java

package stepdefinitions;

import org.junit.runner.RunWith;

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

@RunWith(Cucumber.class)
@CucumberOptions(features="classpath:test.feature", glue = "stepdefinitions", plugin = {"html:target/test-report"})
public class Registration {
  
}

Solution

  • I replaced all the dependencies in my pom.xml file with these dependencies.

    <dependencies>
            <dependency>
                <groupId>io.cucumber</groupId>
                <artifactId>cucumber-java</artifactId>
                <version>7.10.1</version>
            </dependency>
            <dependency>
                <groupId>io.cucumber</groupId>
                <artifactId>cucumber-junit</artifactId>
                <version>7.10.1</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>io.cucumber</groupId>
                <artifactId>cucumber-core</artifactId>
                <version>7.10.1</version>
            </dependency>
        </dependencies>
    

    Then I replaced the import statements in my StepDef.java with

    import io.cucumber.java.en.*
    

    Finally in my Registration.java file, I replaced

    import cucumber.api.CucumberOptions;
    

    with

    import io.cucumber.junit.CucumberOptions;