I'm migrating an enterprise product. The things I have migrated are as follows:
JDK 1.8 to JDK 17
Spring 5.2.2.RELEASE to Spring 6.0.0
Now, in the process of migration, some tests have started to fail.
I have the below dependencies which are in older versions. I have tried to upgrade the versions but it's somehow not working.
Can someone please advise on which version needs to be there to resolve the test cases failures?
pom.xml:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.6</version>
<scope>test</scope>
</dependency>
Test Failure Log:
ClassATest.initializationError » Objenesis java.lang...
ClassBTest.initializationError » Objenesis java.lang.refl...
Running com.util.SupportMonitoringUtilTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec <<< FAILURE! - in com.util.SupportMonitoringUtilTest
initializationError(com.util.SupportMonitoringUtilTest) Time elapsed: 0 sec <<< ERROR!
org.objenesis.ObjenesisException: java.lang.reflect.InvocationTargetException
Caused by: java.lang.reflect.InvocationTargetException
Caused by: java.lang.IllegalAccessError: class jdk.internal.reflect.ConstructorAccessorImpl loaded by org.powermock.core.classloader.MockClassLoader @752573df cannot access jdk/internal/reflect superclass jdk.internal.reflect.MagicAccessorImpl
You need to upgrade/add the following dependencies for PowerMock & Mockito to support JDK 17:
powermock-module-junit4needs to be added. Updatepowermock-module-junit4from1.6.6to2.0.9(which is the latest version at the time of writing the answer).powermock-api-mockitoneeds to be added. Updatepowermock-api-mockito (1.6.6)topowermock-api-mockito2 (2.0.9)(which is the latest version at the time of writing the answer).javassistneeds to be added. Updatejavassistversion to the latest version(3.29.2-GA)(at the time of writing the answer).Note: You might face this error while running the tests on JDK 17:
To avoid this, you will need to tell maven by adding this
--add-opens java.base/java.lang=ALL-UNNAMEDviamaven-surefire-pluginwhen running this.Another point to be noted: You don't need to add Mockito dependency separately.
powermock-api-mockito2comes with themockito-coredependency.pom.xml:
This should work. I have tested on my local, it's working.
Sample codebase example for testing here.