MockClassLoader 无法访问 jdk/internal/reflect superclass jdk.internal.reflect.MagicAccessorImpl
2022-09-01 15:02:34
我正在将一个项目迁移到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
maven-surefire-plugin
<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>
电源模型依赖关系
<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>