Cucumber With JUnit java.lang.ExceptionInInitializerError
Asked Answered
M

2

5

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

Minaret answered 11/4, 2021 at 22:26 Comment(0)
S
7

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] ------------------------------------------------------------------------

Supplemental answered 11/4, 2021 at 23:14 Comment(3)
Yes, I found the XStream error too and try add it directly to maven. I will try it. Should I use the same JUnit version (4.12) ou need update it too? Thanks for your anwser. rgrdsMinaret
@Minaret ideally you should upgrade to JUnit 5, but that's up to you, it works fine now with JUnit 4 like I showed, I used the same version as yoursSupplemental
Yeah. it works fine now with your sugestion. I saw many tutorials with that 1.2.5 version and thinked that was a stable version. My mistake. Thank you very much.. :)Minaret
N
0
I added these dependencies and it worked
<dependencies>
    <dependency>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.8.1</version>
    </dependency>
    <dependency>
      <groupId>info.cukes</groupId>
      <artifactId>cucumber-junit</artifactId>
      <version>1.2.6</version>
    </dependency>
    <dependency>
      <groupId>info.cukes</groupId>
      <artifactId>cucumber-java</artifactId>
      <version>1.2.6</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
    </dependency>
    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-java</artifactId>
      <version>6.10.2</version>
    </dependency>

  </dependencies>
Numbersnumbfish answered 4/1, 2023 at 22:17 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Strow

© 2022 - 2024 — McMap. All rights reserved.