'Using PowerMock in JDK 16

Java reflection is becoming more and more restricted:

  • Up to Java 8 all operations are allowed
  • Starting from Java 9 to 15 you are still able to perform the operations, but you will receive a warning
  • From Java 16 and onwards the operations are forbidden between modules (well, still possible with some special arguments passed to the JVM)

This is a serious problem when using libraries that rely heavily on reflection, like PowerMock that uses it to mock objects in tests.

I created this simple example that illustrates the issue. Here is the pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Powermock</name>

    <properties>
        <java.version>16</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.7.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>2.0.9</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito2</artifactId>
            <version>2.0.9</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <parameters>true</parameters>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

With this simple test SomeTest.java:

package com.example.demo;

import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
class SomeTest {
    @Test
    void contextLoads() {
        Assert.assertEquals(1, 1);
    }
}

Then if we run the command mvn clean test we will get the error:

Running com.example.demo.SomeTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.341 sec <<< FAILURE!
initializationError(com.example.demo.SomeTest)  Time elapsed: 0 sec  <<< ERROR!
java.lang.RuntimeException: java.lang.reflect.InaccessibleObjectException: Unable to make protected native
    java.lang.Object java.lang.Object.clone() throws java.lang.CloneNotSupportedException accessible:
    module java.base does not "opens java.lang" to unnamed module @8b96fde

Strangely if we add this bit to the pom.xml, the problem goes away:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.1</version>
    </parent>

Anybody that can help me with this:

  • How to use PowerMock in JDK 16?
  • Is even PowerMock supported in JDK 16?
  • Is this issue solved using Java modules?
  • Why adding Spring Boot as parent solves the problem?


Solution 1:[1]

You can use PowerMockito with Java 16 by using the --illegal-access=permit option in the Maven surefire plugin. You will see warnings, but it works.

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <argLine>
                        --illegal-access=permit
                    </argLine>
                </configuration>
            </plugin>

For reference this is what I use for Mockito and PowerMockito:

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>3.11.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>2.0.9</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito2</artifactId>
            <version>2.0.9</version>
            <scope>test</scope>
        </dependency>

Solution 2:[2]

Use This plugin in POM file:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>
--add-opens java.base/java.lang=ALL-UNNAMED
--add-opens java.base/java.io=ALL-UNNAMED
--add-opens java.base/java.util=ALL-UNNAMED
--add-opens java.base/java.base=ALL-UNNAMED
</argLine>
</configuration>
</plugin>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tastybento
Solution 2 Efecan AHMETO?LU