MockClassLoader无法访问jdk/internal/reflect超类jdk.internal.reflect.MagicAccessorImpl [英] MockClassLoader cannot access jdk/internal/reflect superclass jdk.internal.reflect.MagicAccessorImpl

查看:1216
本文介绍了MockClassLoader无法访问jdk/internal/reflect超类jdk.internal.reflect.MagicAccessorImpl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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>

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 ,但是对于Java 9来说应该可以:

It is an (currently) open issue @powermock, but for Java 9 this should work:

<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>

请参阅: https://github.com/powermock/powermock/issues/901#issuecomment-385533096

使用Java 11, Mosheer-Ahmad 设法运行了测试,带有:

With Java 11, Mosheer-Ahmad managed to run his tests, with:

  1. 上述依赖项,
  2. org.javassist javassist 3.24.1-GA test
  3. 的其他依赖
  4. 此(测试基础)类/注释:

  1. the above dependencies,
  2. an additional dependency on org.javassist javassist 3.24.1-GA testand
  3. 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 {

}

.

这篇关于MockClassLoader无法访问jdk/internal/reflect超类jdk.internal.reflect.MagicAccessorImpl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆