Skip to content
Advertisement

Cucumber With JUnit java.lang.ExceptionInInitializerError

I’m new to the UnitTesting and Cucumber, and today I tried to implement a simple example from a tutorial in Intelij and Eclipse and I got the same error when I try run the TestRunner.java.

My pom.xml:

<dependencies> 
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>1.2.5</version>
</dependency>
<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>
</dependency>

File .feature

   Feature: User Login
  User should be able to login using valid credentials


  Scenario: Testing login with valid credentials
    Given I am on login page
    When I enter username as "jsmith" and password as "demo1234"
    And I submit login page
    Then I redirect to user home page

TestRunner.java

    package com.unit.runner;

import org.junit.runner.RunWith;

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

@RunWith(Cucumber.class)
@CucumberOptions(features = "classpath:login/LoginTest.feature",
glue = "com.unit.runner.steps")
public class TestRunner {

}

Steps

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

public class StepDefinationSteps {
    
    @Given("^I am on login page$")
    public void i_am_on_login_page() throws Throwable {
        System.out.println("open login page url");
    }

    @When("^I enter username as "([^"]*)" and password as "([^"]*)"$")
    public void i_enter_username_as_and_password_as(String username, String password) throws Throwable {
        System.out.println("open login page url");
    }

    @When("^I submit login page$")
    public void i_submit_login_page() throws Throwable {
        System.out.println("open login page url");
    }

    @Then("^I redirect to user home page$")
    public void i_redirect_to_user_home_page() throws Throwable {
        System.out.println("open login page url");
    }

}

My file structure:

enter image description here

And the error:

1 Scenarios (1 failed) 4 Steps (1 failed, 3 skipped) 0m0,225s

java.lang.ExceptionInInitializerError … Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.util.Comparator java.util.TreeMap.comparator accessible: module java.base does not “opens java.util” to unnamed module @378bf509

Advertisement

Answer

The Cucumber version you’re using is very outdated.

It still contained the XStream library which had this buggy behaviour.

XStream has been removed from Cucumber since version 3

Cucumber 1.x and 2.x used a library called XStream as a central building block for both data tables and type conversion.

However the usage of XStream in combination with Cucumber was poorly documented and it did not allow for the use of other Object Mappers (e.g. Jackson) which made it impossible to reuse domain objects. As XStream is not compatible with Java 9 it was also problem in long term.

Updated your dependencies to

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>6.10.2</version>
</dependency>
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>6.10.2</version>
</dependency>

Then you’ll have to update your different imports to include those instead because the packages have changed

// In StepDefinitionSteps.java
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
// In TestRunner.java
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

When this is all done, I get the expected printed out when performing a mvn test

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.unit.runner.TestRunner
open login page url
open login page url
open login page url
open login page url
?????????????????????????????????????????????????????????????????????????????????????
? Share your Cucumber Report with your team at https://reports.cucumber.io          ?
? Activate publishing with one of the following:                                    ?
?                                                                                   ?
? src/test/resources/cucumber.properties:          cucumber.publish.enabled=true    ?
? src/test/resources/junit-platform.properties:    cucumber.publish.enabled=true    ?
? Environment variable:                            CUCUMBER_PUBLISH_ENABLED=true    ?
? JUnit:                                           @CucumberOptions(publish = true) ?
?                                                                                   ?
? More information at https://reports.cucumber.io/docs/cucumber-jvm                 ?
?                                                                                   ?
? Disable this message with one of the following:                                   ?
?                                                                                   ?
? src/test/resources/cucumber.properties:          cucumber.publish.quiet=true      ?
? src/test/resources/junit-platform.properties:    cucumber.publish.quiet=true      ?
?????????????????????????????????????????????????????????????????????????????????????
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.562 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.807 s
[INFO] Finished at: 2021-04-12T01:11:09+02:00
[INFO] ------------------------------------------------------------------------

Advertisement