MockClassLoader无法访问jdk / internal / reflect超类jdk.internal.reflect.MagicAccessorImpl

时间:2018-05-21 21:16:13

标签: java maven powermock powermockito

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

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>

5 个答案:

答案 0 :(得分:3)

请,&#34;尝试&#34;:

<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-core</artifactId>
  <version>2.18.0</version>
</dependency>
<dependency>
  <groupId>org.powermock</groupId>
  <artifactId>powermock-api-mockito2</artifactId>
  <version>2.0.0-beta.5</version>
</dependency>
<dependency>
  <groupId>org.powermock</groupId>
  <artifactId>powermock-module-junit4</artifactId>
  <version>2.0.0-beta.5</version>
</dependency>

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

答案 1 :(得分:3)

我必须编译一个使用powermock进行测试的第三方jar。为了解决此错误,我必须添加:

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

对于使用powermock测试的类

答案 2 :(得分:2)

只是重申sghaier ali提出的非常好的观点。

在被忽略的类中添加*可解决此问题。

所做的更改是:

  • 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.*"})
    

答案 3 :(得分:0)

正如@ smac89所述,我要做的就是忽略有问题的软件包。

通过用@PowerMockIgnore("jdk.internal.reflect.*")注释我的测试班

我的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版本:

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)

答案 4 :(得分:0)

尝试使用Oracle JDK 1.8运行它