JAVA 17 Junit test cases failing with java.lang.reflect.InaccessibleObjectException

5k Views Asked by At

java version:17 mockito version:5.3.0

As I am upgrading to java 17, junit test cases are failing with reason:

Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @38cccef

Please suggest some solution as I CANNOT use solutions:

  1. —add-opens java.base/java.lang=ALL-UNNAMED or
  2. --illegal-access=permit (removed in java 17)

I am expecting to solve this problem by adding any new dependency in pom.xml or different version of mockito. with minimal code change.

2

There are 2 best solutions below

0
rzwitserloot On

Follow the instructions in the mockito release notes under '5.0.0' here. Essentially, switch the mockmaker implementation.

That, or downgrade back to JDK11.

1
Amit Kumar On

add this to pom:

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