malejpavouk
malejpavouk

Reputation: 4445

PowerMock & Java 11

We are using PowerMock in few of our historical projects. Unfortunately PowerMock is quite dead and is not compatible with Java 11.

And we are using mockStatic(). Yes, we know its considered harmful - its in the legacy code and we would prefer not to rewrite those classes now...

Are there any options how to tweak PowerMock to support Java 11? Or is it possible to easily replace it with some other Java 11 compatible framework? (Mockito does not support mockStatic)

Upvotes: 34

Views: 43957

Answers (7)

Bruno Bossola
Bruno Bossola

Reputation: 1378

Resolved with:

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

Nothing else needed. You can put it on the top of the test class, with the other annotations, as in this example:

@RunWith(PowerMockRunner.class)
@PowerMockIgnore("jdk.internal.reflect.*")
@PrepareForTest({ File.class, FileSystemUtils.class })
public class MyTest {
   ...
}

Upvotes: 20

jngcs
jngcs

Reputation: 11

If use reflect in JDK11, you might see some warning like 'An illegal reflective access operation has occurred', because reflect JDK inner API is illegal since JDK9, still you can use it with a warning above.

To fix the problem temporary, try to use --add-exports or --add-opens in your argLing.

For example: --add-opens java.xml/jdk.xml.internal=ALL-UNNAMED

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>3.0.0-M5</version>
  <configuration>
    <argLine>
      @{argLine} --add-opens java.xml/jdk.xml.internal=ALL-UNNAMED 
    </argLine>
  </configuration>
</plugin>

For more infomation, watch --add-opens introduce

Upvotes: 1

Sachin Pete
Sachin Pete

Reputation: 21

if your using java 11 then downgrade the dependency org.power.mock to 1.7.0.

Upvotes: 1

user327961
user327961

Reputation: 2490

If there are a lot of test classes to migrate to Java 11, it is possible to configure the ignores globally (only once in a configuration file): org/powermock/exntensions/configuration.properties

powermock.global-ignore=com.sun.org.apache.xerces.*,javax.xml.*,org.xml.*,org.w3c.*

Upvotes: 2

Saurabh Chaudhari
Saurabh Chaudhari

Reputation: 76

From mockito version 3.4.0 it is possible to mock static methods. You can refer to https://www.baeldung.com/mockito-mock-static-methods for more info.

Upvotes: 3

Chen Daocheng
Chen Daocheng

Reputation: 59

add the properties: org/powermock/extensions/configuration.properties

https://github.com/powermock/powermock/wiki/PowerMock-Configuration https://github.com/powermock/powermock-examples-maven/blob/master/global-ignore/src/test/resources/org/powermock/extensions/configuration.properties

e.g. for maven/gradle: src/test/resources/org/powermock/extensions/configuration.properties

powermock.global-ignore=javax.crypto.*,com.sun.xml.internal.stream.*,javax.xml.stream.*,javax.net.ssl.*,org.slf4j.*,javax.xml.parsers.*,ch.qos.logback.*,jdk.xml.internal.*,com.sun.org.apache.xerces.*,java.xml.*,org.xml.*,javax.management.*,org.w3c.dom.*

Upvotes: 2

malejpavouk
malejpavouk

Reputation: 4445

After one year of no releases, things are really moving in PowerMock.

PowerMock 2.0.0-RC1 was released. And with PowerMockito 2.0.0-RC1 + @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})

The tests work under Java 11.

Upvotes: 29

Related Questions