黄瓜与 JUnit java.lang.ExceptionInInitializerError
Posted
技术标签:
【中文标题】黄瓜与 JUnit java.lang.ExceptionInInitializerError【英文标题】:Cucumber With JUnit java.lang.ExceptionInInitializerError 【发布时间】:2021-07-07 02:00:07 【问题描述】:我是 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
【问题讨论】:
【参考方案1】:您使用的 Cucumber 版本已经过时了。
它仍然包含具有this buggy behaviour 的 XStream 库。
XStream has been removed from Cucumber since version 3
Cucumber 1.x 和 2.x 使用一个名为 XStream 的库作为数据表和类型转换的核心构建块。
但是,XStream 与 Cucumber 结合使用的文档很少,并且不允许使用其他对象映射器(例如 Jackson),这使得无法重用域对象。由于 XStream 与 Java 9 不兼容,因此长期来看也是个问题。
将您的依赖项更新为
<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] ------------------------------------------------------------------------
【讨论】:
是的,我也发现了 XStream 错误并尝试将其直接添加到 maven。我会试试看。我应该使用相同的 JUnit 版本(4.12)吗?你也需要更新它吗?谢谢你的回答。 rgrds @Lnoob 理想情况下你应该升级到 JUnit 5,但这取决于你,它现在可以像我展示的那样在 JUnit 4 上正常工作,我使用与你相同的版本 是的。现在可以根据您的建议正常工作。我看了很多关于那个 1.2.5 版本的教程,并认为这是一个稳定的版本。我的错。非常感谢.. :)以上是关于黄瓜与 JUnit java.lang.ExceptionInInitializerError的主要内容,如果未能解决你的问题,请参考以下文章