35

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

<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>
xerx593
  • 12,237
  • 5
  • 33
  • 64
Arar
  • 1,926
  • 5
  • 29
  • 47

5 Answers5

63

I had a test dependency on a third-party jar which used powermock. In order to resolve this error, I had to add:

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

To the class that is tested with powermock

smac89
  • 39,374
  • 15
  • 132
  • 179
  • 1
    but still I am facing some issue Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected java.lang.Class jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(java.lang.String,boolean) throws java.lang.ClassNotFoundException accessible: module java.base does not "opens jdk.internal.loader" to unnamed module @506c589e – Sandeep Kamath Apr 12 '20 at 01:04
  • 1
    @SandeepKamath or Saif Masadeh how did you get around this? I am facing same problem with jdk11. and ignoring jdk.internal.reflect.* does not works for me. – mohd shoaib Mar 11 '21 at 16:33
  • 1
    @mohdshoaib I have the same issue. Did you solve it? – Wenaro Apr 29 '21 at 11:02
  • 1
    hey @Wenaro, I had to add jdk.internal.reflect.* in the ignore list. But also had to update my JaCoCo plugin version and if it's a multi-module project then made fork-count as zero for the surefire plugin. Essentially different thing might work for your uses. Adding entire ignore list if that helps: @PowerMockIgnore({"javax.management.*", "javax.xml.*", "org.xml.sax.*", "org.w3c.dom.*", "org.springframework.context.*", "org.apache.log4j.*", "org.apache.commons.logging.*", "org.jacoco.*", "jdk.internal.reflect.*"}) – mohd shoaib May 04 '21 at 09:20
17

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>

See: https://github.com/powermock/powermock/issues/901#issuecomment-385533096


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

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

    .

xerx593
  • 12,237
  • 5
  • 33
  • 64
  • best answered by the response from the "main contributor" of powermock: " thekingnothing commented on 20 Apr 2018 Hi, There is some sort of supporting JKD 9 in beta versions of PowerMock 2.0. I’m not going to implement supporting Java 9 or 10 in the PowerMock 1.x Right now the project is in frozen state because I do not have enough time. I hope I will be able to return back to project soon. Best Regards, Arthur Zagretdinov ..." from: https://github.com/powermock/powermock/issues/901 – xerx593 Jul 30 '19 at 16:13
  • 2
    If you are (still) stuck with that, please also try [this comment](https://github.com/powermock/powermock/issues/901#issuecomment-510922977). – xerx593 Jul 30 '19 at 16:16
  • yes, upgrading version of `powermockito` solve. FYI, I am on `jdk 9` – roottraveller Jul 31 '19 at 05:31
  • 1
    is work for me this solution but i have add * like this: @PowerMockIgnore({"javax.management.", "com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.dom.*", "com.sun.org.apache.xalan.*", "javax.activation.*"}) – sghaier ali Mar 13 '20 at 16:03
  • thx, @sghaierali! I think the contents of `@PowerMockIgnore` are also somewhat "custom", everyone has to find his (own, minimum) "workaround set". – xerx593 Mar 13 '20 at 18:08
6

Just to re-iterate the very good point made by 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.*"})
    
Felk
  • 7,720
  • 2
  • 35
  • 65
Rajesh Goel
  • 3,277
  • 1
  • 17
  • 13
  • I believe that the ignoring of these specific packages seems more correct before reflection seems key to mocking. When I include this ignore, 2 of my 3 instances of the error go away - I still have one instance of the error. If I simply ignore reflection this is flagged (i.e., the kind of behavior central to mocking): `when(mockBatchHandler.handleMessageType(any(), any(), any(), any())).thenReturn(taskMap);` – Dr Dave Mar 02 '22 at 16:58
5

As @smac89 mentioned all I had to do is ignore the offending package.

By annotating my test class with @PowerMockIgnore("jdk.internal.reflect.*")

My Maven dependencies are:

    <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)
DaddyMoe
  • 990
  • 1
  • 14
  • 20
1

Try to run it using Oracle JDK 1.8

Alexandre
  • 363
  • 7
  • 20
  • As suggested by you, changed to java 8 and it worked fine. I am using Ubuntu, so ran command and picked java 8 version sudo update-alternatives --config java – Arif Shaikh Apr 07 '21 at 11:31