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

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

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

问题

  1. java版本:17 mockito版本:5.3.0
  2. 由于我正在升级到java 17junit测试用例失败的原因是:
  3. > 由于模块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可访问。
  4. 请建议一些解决方案,因为我不能使用以下解决方案:
  5. 1. add-opens java.base/java.lang=ALL-UNNAMED
  6. 或者
  7. 2. --illegal-access=permit (在java 17中已删除)
  8. 我期望通过在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文件中的部分翻译如下:

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.apache.maven.plugins</groupId>
  5. <artifactId>maven-surefire-plugin</artifactId>
  6. <version>3.1.0</version>
  7. <configuration>
  8. <argLine>--add-opens java.base/java.lang=ALL-UNNAMED</argLine>
  9. </configuration>
  10. </plugin>
  11. </plugins>
  12. </build>
英文:

add this to pom:

  1. &lt;build&gt;
  2. &lt;plugins&gt;
  3. &lt;plugin&gt;
  4. &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
  5. &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
  6. &lt;version&gt;3.1.0&lt;/version&gt;
  7. &lt;configuration&gt;
  8. &lt;argLine&gt;--add-opens java.base/java.lang=ALL-UNNAMED&lt;/argLine&gt;
  9. &lt;/configuration&gt;
  10. &lt;/plugin&gt;
  11. &lt;/plugins&gt;
  12. &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:

确定