gpt4 book ai didi

java - cucumber 与 JUnit java.lang.ExceptionInInitializerError

转载 作者:行者123 更新时间:2023-12-04 13:09:31 24 4
gpt4 key购买 nike

我是 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
测试运行程序
    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");
}

}
我的文件结构:
enter image description here
和错误:
1 个场景(1 个失败)
4 步(1 失败,3 跳过)
0m0,225s
java.lang.ExceptionInInitializerError ...
引起:java.lang.reflect.InaccessibleObjectException:无法使字段私有(private)最终java.util.Comparator java.util.TreeMap.comparator可访问:模块java.base不会“打开java.util”到未命名的模块@378bf509

最佳答案

您使用的 Cucumber 版本非常过时。
它仍然包含具有 this buggy behaviour 的 XStream 库。 .
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] ------------------------------------------------------------------------

关于java - cucumber 与 JUnit java.lang.ExceptionInInitializerError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67050745/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com