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>
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
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:
org.javassist javassist 3.24.1-GA testandthis (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 {
}
.
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.*"})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With