从 Java 8 迁移到 Java 17 和 Spring 6 JUnit4 由于版本不匹配而失败

问题描述 投票:0回答:1

我已将 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
spring junit mockito powermock java-17
1个回答
0
投票

将以下依赖项添加到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>
    </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。

我已经测试过它可以工作。

测试代码:

    import org.junit.Assert;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.mockito.Mockito;
    import org.powermock.api.mockito.PowerMockito;
    import org.powermock.core.classloader.annotations.PrepareForTest;
    import org.powermock.modules.junit4.PowerMockRunner;
    import static org.mockito.Mockito.times;
    
    @RunWith(PowerMockRunner.class)
    @PrepareForTest({Demo.class})
    class Test {
    
        public Test() {
    
        }
    
        @Test
        public void test1() {
            PowerMockito.mockStatic(Demo.class);
            Mockito.when(Demo.message()).thenReturn("Anything!");
            String message = Demo.message();
            Assert.assertEquals(message, "Anything!");
            PowerMockito.verifyStatic(Demo.class, times(1));
            Demo.message();
        }
    
    }

class Demo {

    public static String message() {
        return "Hi!";
    }

}

截图:

© www.soinside.com 2019 - 2024. All rights reserved.