sanchit relan
sanchit relan

Reputation: 281

Powermock Compatibility with JDK 17

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.

Upvotes: 28

Views: 43421

Answers (4)

Cheok Yan Cheng
Cheok Yan Cheng

Reputation: 42768

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

android {
    ...

    testOptions {
        ...

        // https://stackoverflow.com/questions/69896191/powermock-compatibility-with-jdk-17
        unitTests.all {
            jvmArgs += ['--add-opens=java.base/java.lang=ALL-UNNAMED', '--add-opens=java.base/java.util=ALL-UNNAMED']
        }
    }
}

Upvotes: 2

Theri Muthu Selvam
Theri Muthu Selvam

Reputation: 162

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

Upvotes: 1

mbwasi
mbwasi

Reputation: 4307

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

Upvotes: 17

julmud
julmud

Reputation: 391

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>

Upvotes: 29

Related Questions