我正在迁移企业产品。我迁移的东西如下:
JDK 1.8 到 JDK 17
Spring 5.2.2.RELEASE 到 Spring 6.0.0
现在,在迁移过程中,一些测试开始失败。
我有以下旧版本的依赖项。我尝试过升级版本,但不知何故不起作用。
有人可以建议需要哪个版本来解决测试用例失败问题吗?
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>
测试失败日志:
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:
- 新版本
powermock-module-junit4
需要添加。更新powermock-module-junit4
来自1.6.6
至2.0.9
(这是撰写答案时的最新版本)。 - 新版本
powermock-api-mockito
需要添加。更新powermock-api-mockito (1.6.6)
至powermock-api-mockito2 (2.0.9)
(这是撰写答案时的最新版本)。 - 新版本
javassist
需要添加。更新javassist
版本到最新版本(3.29.2-GA)
(在撰写答案时)。
注意:在 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通过maven-surefire-plugin
运行时。
还有一点需要注意:你不需要单独添加Mockito依赖。 powermock-api-mockito2
附带 mockito-core
依赖性。
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>
<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>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.29.2-GA</version>
</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>
这应该有效。我已经在本地测试过,可以正常工作。
用于测试的示例代码库示例 here .
关于java - 从 Java 8 迁移到 Java 17 和 Spring 6 JUnit4 由于版本不匹配而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77234542/