我已将 Java 版本 1.8 迁移到 Java 17,并从 Spring 5.2.22.RELEASE 迁移到 Spring 6.0.0,现在我遇到了一些测试失败 我有以下旧版本的依赖项,尝试升级版本但不起作用,您能否建议需要哪个版本来解决失败测试用例
<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>
测试错误: 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
您必须升级您的 PowerMock 和 Mockito 版本才能支持 JDK 17 上的 JUnit4。
将以下依赖项添加到pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo-2</name>
<description>Sample</description>
<properties>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.9</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<argLine>
--add-opens java.base/java.lang=ALL-UNNAMED
</argLine>
</configuration>
</plugin>
</plugins>
</build>
</project>
如果你看到这个,我已经升级了:
powermock-module-junit4
从 1.6.6
到 2.0.9
powermock-api-mockito (1.6.6)
至 powermock-api-mockito2 (2.0.9)
注意: 在 JDK 17 上运行测试时,您可能会遇到此错误:
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
为了避免这种情况,你需要在运行时通过
--add-opens java.base/java.lang=ALL-UNNAMED
添加这个 maven-surefire-plugin
来告诉 maven。
我已经测试过它可以工作。
截图: