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>

答案 1

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

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

到 测试的类powermock


答案 2

这是一个(目前)@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>

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


在Java 11中,Mosheer-Ahmad设法运行了他的测试,包括:

  1. 上述依赖关系,
  2. 对 和 的额外依赖org.javassist javassist 3.24.1-GA test
  3. 这个(测试基)类/注释:

    @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 {
    
    }
    

    .


推荐