Java MockClassLoader 无法访问 jdk/internal/reflect 超类 jdk.internal.reflect.MagicAccessorImpl
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50456726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MockClassLoader cannot access jdk/internal/reflect superclass jdk.internal.reflect.MagicAccessorImpl
提问by Saif Masadeh
I am in the middle of migrating a project into Java9, The Tests start failing after I switched to the new Java version, it seems like PowerMock is trying to access some classes it does not have access to.
我正在将一个项目迁移到 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
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>
powermock dependency
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>
回答by xerx593
It is an (currently) open issue @powermock, but for Java 9 this should work:
这是一个(当前)未解决的问题 @powermock,但对于 Java 9,这应该有效:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.18.0</version> <!-- or higher, correspondning to powermock-version -->
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.0-beta.5</version> <!-- or higher -->
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.0-beta.5</version> <!-- or higher -->
</dependency>
See: https://github.com/powermock/powermock/issues/901#issuecomment-385533096
参见:https: //github.com/powermock/powermock/issues/901#issuecomment-385533096
With Java 11, Mosheer-Ahmadmanaged to run his tests, with:
在 Java 11 中,Mosheer-Ahmad设法运行了他的测试:
- the above dependencies,
- an additional dependency on
org.javassist javassist 3.24.1-GA test
and this (test base) class/annotations:
@RunWith(PowerMockRunner.class) @PowerMockIgnore({"javax.management.", "com.sun.org.apache.xerces.", "javax.xml.", "org.xml.", "org.w3c.dom.", "com.sun.org.apache.xalan.", "javax.activation.*"}) public class PowerMockitoBaseRunner { }
.
- 上面的依赖,
- 对
org.javassist javassist 3.24.1-GA test
和的额外依赖 这个(测试基础)类/注释:
@RunWith(PowerMockRunner.class) @PowerMockIgnore({"javax.management.", "com.sun.org.apache.xerces.", "javax.xml.", "org.xml.", "org.w3c.dom.", "com.sun.org.apache.xalan.", "javax.activation.*"}) public class PowerMockitoBaseRunner { }
.
回答by smac89
I had a test dependency on a third-party jar which used powermock. In order to resolve this error, I had to add:
我对使用 powermock 的第三方 jar 有测试依赖性。为了解决这个错误,我不得不添加:
@PowerMockIgnore("jdk.internal.reflect.*")
To the class that is tested with powermock
到被测试的班级 powermock
回答by DaddyMoe
As @smac89 mentioned all I had to do is ignore the offending package.
正如@smac89 提到的,我所要做的就是忽略有问题的包。
By annotating my test class with @PowerMockIgnore("jdk.internal.reflect.*")
通过注释我的测试类 @PowerMockIgnore("jdk.internal.reflect.*")
My Maven dependencies are:
我的 Maven 依赖项是:
<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>
</dependency>
Java version:
爪哇版:
java -version
openjdk version "11.0.1" 2018-10-16
OpenJDK Runtime Environment 18.9 (build 11.0.1+13)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode)
回答by Rajesh Goel
Just to re-iterate the very good point made by sghaier ali.
只是重申 sghaier ali 提出的非常好的观点。
Adding * to the ignored classes solved the issue.
将 * 添加到被忽略的类解决了这个问题。
changes done are:
所做的更改是:
The following dependencies in build.gradle:
testImplementation 'org.mockito:mockito-core:3.3.3'
testImplementation 'org.powermock:powermock-api-mockito2:2.0.5'
testImplementation 'org.powermock:powermock-module-junit4:2.0.5'annotating the specific class with:
@PowerMockIgnore({"javax.management.", "com.sun.org.apache.xerces.", "javax.xml.", "org.xml.", "org.w3c.dom.", "com.sun.org.apache.xalan.", "javax.activation.*"})
build.gradle 中的以下依赖项:
testImplementation 'org.mockito:mockito-core:3.3.3'
testImplementation 'org.powermock:powermock-api-mockito2:2.0.5'
testImplementation 'org.powermock:powermock-module-junit4: 2.0.5'注释特定的类:
@PowerMockIgnore({"javax.management. ", "com.sun.org.apache.xerces.", "javax.xml. ", "org.xml.", "org.w3c.dom . ", "com.sun.org.apache.xalan.", "javax.activation.*"})