带有 JUnit 的黄瓜 java.lang.ExceptionInInitializerError

Cucumber With JUnit java.lang.ExceptionInInitializerError

我是 UnitTesting 和 Cucumber 的新手,今天我尝试在 Intelij 和 Eclipse 中实现教程中的一个简单示例,当我尝试 运行 TestRunner.java.

我的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>

文件.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 {

}

步骤

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");
    }

}

我的文件结构:

错误:

1 个场景(1 个失败) 4 个步骤(1 个失败,3 个跳过) 0m0,225s

java.lang.ExceptionInInitializerError ... 由以下原因引起:java.lang.reflect.InaccessibleObjectException:无法使字段私有最终 java.util.Comparator java.util.TreeMap.comparator 可访问:模块 java.base 不会“打开 java.util” 未命名模块 @378bf509

您使用的 Cucumber 版本非常过时。

它仍然包含 XStream 库,其中有 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.

已将您的依赖项更新为

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

然后你必须更新你的不同导入以包含那些,因为包已经改变

// 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;

当这一切都完成后,我在执行 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] ------------------------------------------------------------------------