Powermock Compatibility with JDK 17
Asked Answered
V

4

24

Recently I was upgrading my project from JDK 11 to JDK 17. After upgrading, powermock seems to have an issue. While running AUT's , I am getting following error:

java.lang.RuntimeException: PowerMock internal error: Should never throw exception at this level
Caused by: 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 @3fc34119

Do you know any workaround this issue, If so can you please provide the solution.

Vickievicksburg answered 9/11, 2021 at 10:3 Comment(5)
Have you taken a look if there are newer versions of powermockito that might be required for more recent versions of java?Weatherworn
Could it be that you encountered open issue 1099 and/or 1094?Pledge
I am using the latest 2.0.9 powermock versionVickievicksburg
If you want to stay up-to-date with recent JDK versions, I suggest to stay with mockito. Historically, Powermock has taken longer time to keep up, while mockito has a bigger ecosystem around it, both in terms of users and developers.Thrombin
After encountering this incompatibility once again, I've used it as an opportunity to review whether I truly needed Powermock. In my case, I was only using it for mocking static classes, which is since supported in Mockito.Platypus
P
27

As a stop gap measure (until Powermock gets updated), you should be able to run your tests by passing the following argument to your JVM:

--add-opens java.base/java.lang=ALL-UNNAMED

If you're running your tests with Maven, you can configure the surefire-plugin like this:

<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>${plugin.surefire.version}</version>
  <configuration>
    <argLine>--add-opens java.base/java.lang=ALL-UNNAMED</argLine>
  </configuration>
</plugin>
Picnic answered 21/2, 2022 at 8:47 Comment(7)
Also you can use these: --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-UNNAMEDCassidycassie
Worked for me (JDK 17)Martinmas
how to use it in gradle?Promethean
@ImtiazAli this worked for me: test { jvmArgs "--add-opens", "java.base/java.lang=ALL-UNNAMED" }Sawn
How to make this work in case I want to run the tests in Intellij? In other words, if I right click on the test class, then I click run, it will still throw the exception mentioned above.Denigrate
@Denigrate If you re using gradle check my answer.Jules
@MA I am also facing while debugging with intellij. Can you tell me if you got any successBetty
J
15

This shoudl allow the solution to work with tests ran from inside the IDE, Android Studio in my case.

I had to do this because PowerMock doesn't play nice with Java 17 on Android.

In your top level project build.gradle, at the bottom just add

subprojects{
    tasks.withType(Test).configureEach{
        jvmArgs = jvmArgs + ['--add-opens=java.base/java.lang=ALL-UNNAMED']
    }
}

If you are using Kotlin for your Gradle files see https://github.com/square/okhttp/blob/f9901627431be098ad73abd725fbb3738747461c/build.gradle.kts#L153

Jules answered 16/5, 2023 at 21:58 Comment(2)
this is a great answer, it fixed Android Studio and my failing Jenkins build!Misconduct
It works for Robolectric too. ThanksRepute
H
1

Try the following in your app level build.gradle, if you are still having issue.

android {
    ...

    testOptions {
        ...

        // https://mcmap.net/q/547667/-powermock-compatibility-with-jdk-17
        unitTests.all {
            jvmArgs += ['--add-opens=java.base/java.lang=ALL-UNNAMED', '--add-opens=java.base/java.util=ALL-UNNAMED']
        }
    }
}
Haas answered 3/7 at 3:37 Comment(0)
W
0

Go to the Run Configurations in your Eclipse IDE and choose Arguments tab and in VM arguments add the below and run the application.

--add-opens java.base/java.lang=ALL-UNNAMED

We can add other VM arguments too if there is any issue with util and text packages as below respectively,

--add-opens java.base/java.util=ALL-UNNAMED
--add-opens java.base/java.text=ALL-UNNAMED
Wheeze answered 3/4 at 13:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.