gpt4 book ai didi

java - MockClassLoader无法访问jdk/internal/reflect父类(super class)jdk.internal.reflect.MagicAccessorImpl

转载 作者:行者123 更新时间:2023-12-01 18:01:55 26 4
gpt4 key购买 nike

我正在将一个项目迁移到 Java9,在我切换到新的 Java 版本后,测试开始失败,看起来 PowerMock 正在尝试访问一些它无法访问的类。

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.973 sec <<< FAILURE! - in com.Test
initializationError(com.Test) Time elapsed: 0.007 sec <<< ERROR!
org.objenesis.ObjenesisException: java.lang.reflect.InvocationTargetException
Caused by: java.lang.reflect.InvocationTargetException
Caused by: java.lang.IllegalAccessError: class jdk.internal.reflect.ConstructorAccessorImpl loaded by org/powermock/core/classloader/MockClassLoader cannot access jdk/internal/reflect superclass jdk.internal.reflect.MagicAccessorImpl

ma​​ven-surefire-插件

<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<includes>
<include>**/*Test.java</include>
<include>**/*Test.groovy</include>
<include>**/*Spec.*</include>
</includes>
<forkMode>always</forkMode>
<argLine>--add-modules java.xml.bind</argLine>
<argLine>--add-modules java.activation</argLine>
<argLine>--add-opens=java.base/java.lang=ALL-UNNAMED --illegal-access=warn</argLine>
</configuration>
</plugin>

powermock 依赖

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.7.4</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.7.4</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
</exclusion>
</exclusions>
</dependency>

最佳答案

我对使用 powermock 的第三方 jar 进行了测试依赖。为了解决这个错误,我必须添加:

@PowerMockIgnore("jdk.internal.reflect.*")

到使用powermock测试的类

关于java - MockClassLoader无法访问jdk/internal/reflect父类(super class)jdk.internal.reflect.MagicAccessorImpl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60613446/

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