JAVA 17 Junit测试用例失败,出现java.lang.reflect.InaccessibleObjectException。

huangapple go评论61阅读模式
英文:

JAVA 17 Junit test cases failing with java.lang.reflect.InaccessibleObjectException

问题

java版本:17 mockito版本:5.3.0

由于我正在升级到java 17,junit测试用例失败的原因是:

> 由于模块java.base未“opens java.lang”给未命名模块@38cccef,无法使受保护的最终java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError可访问。

请建议一些解决方案,因为我不能使用以下解决方案:
1. —add-opens java.base/java.lang=ALL-UNNAMED
或者
2. --illegal-access=permit (在java 17中已删除)

我期望通过在pom.xml中添加新的依赖项或者使用不同版本的mockito来解决这个问题,同时尽量减少代码修改。
英文:

java version:17 mockito version:5.3.0

As I am upgrading to java 17, junit test cases are failing with reason:

> Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @38cccef

Please suggest some solution as I CANNOT use solutions:

  1. —add-opens java.base/java.lang=ALL-UNNAMED
    or
  2. --illegal-access=permit (removed in java 17)

I am expecting to solve this problem by adding any new dependency in pom.xml or different version of mockito. with minimal code change.

答案1

得分: 2

添加到pom文件中的部分翻译如下:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <argLine>--add-opens java.base/java.lang=ALL-UNNAMED</argLine>
            </configuration>
        </plugin>
    </plugins>
</build>
英文:

add this to pom:

&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
&lt;version&gt;3.1.0&lt;/version&gt;
&lt;configuration&gt;
&lt;argLine&gt;--add-opens java.base/java.lang=ALL-UNNAMED&lt;/argLine&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;

答案2

得分: 0

Follow the instructions in the mockito release notes under '5.0.0' here. Essentially, switch the mockmaker implementation.

或者降级回到 JDK11。

英文:

Follow the instructions in the mockito release notes under '5.0.0' here. Essentially, switch the mockmaker implementation.

That, or downgrade back to JDK11.

huangapple
  • 本文由 发表于 2023年5月17日 21:37:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272738.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定