15

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.

gjoranv
  • 4,376
  • 3
  • 21
  • 37
sanchit relan
  • 151
  • 1
  • 1
  • 5
  • 2
    Have you taken a look if there are newer versions of powermockito that might be required for more recent versions of java? – f1sh Nov 09 '21 at 10:05
  • 2
    Could it be that you encountered open issue [1099](https://github.com/powermock/powermock/issues/1099) and/or [1094](https://github.com/powermock/powermock/issues/1094)? – Hulk Nov 09 '21 at 10:29
  • I am using the latest 2.0.9 powermock version – sanchit relan Nov 09 '21 at 11:34
  • 3
    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. – gjoranv Nov 09 '21 at 13:44
  • 2
    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. – Paul Lammertsma Mar 26 '23 at 15:51

2 Answers2

17

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>
Sadra
  • 2,480
  • 2
  • 20
  • 32
julmud
  • 171
  • 1
  • 3
  • 7
3

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

mbwasi
  • 3,612
  • 3
  • 30
  • 36