You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maven Surefire插件未读取JVM选项,JDK17升级后测试报错求助

问题

将应用升级至JDK17后,测试时报错:

java.lang.reflect.InaccessibleObjectException: 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

在IntelliJ中修改测试配置,添加VM选项--add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.lang.reflect=ALL-UNNAMED --add-opens java.base/java.util.concurrent=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED可解决该错误,但在pom.xml的maven-surefire-plugin中仅配置单个<argLine>参数时,Maven测试仍报错,相关配置如下:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.1.2</version>
            <configuration>
                <skipTests>${skip.surefire.integration.tests}</skipTests>
                <includes>
                    <include>**/*Test.java</include>
                </includes>
                <forkCount>10</forkCount>
                <reuseForks>false</reuseForks>
                <argLine>--add-opens java.base/java.lang=ALL-UNNAMED</argLine>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>com.salesforce.atf</groupId>
                    <artifactId>salesforce-test-client-provider</artifactId>
                    <version>1.0.1</version>
                    <exclusions>
                        <exclusion>
                            <groupId>org.junit.vintage</groupId>
                            <artifactId>junit-vintage-engine</artifactId>
                        </exclusion>
                    </exclusions>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>com.salesforce.atf</groupId>
            <artifactId>testclient-report-plugin</artifactId>
            <version>0.0.1</version>
            <executions>
                <execution>
                    <phase>verify</phase>
                    <goals>
                        <goal>stc-report-only</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
解决方法
  • 补充完整--add-opens参数:你在IDEA中使用了4个--add-opens参数,但Maven配置仅添加了1个,缺失的参数会导致部分反射操作仍无法访问模块内部API。需将所有必要参数加入argLine:

    <argLine>--add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.lang.reflect=ALL-UNNAMED --add-opens java.base/java.util.concurrent=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED</argLine>
    
  • 避免覆盖原有argLine配置:如果项目中其他插件或环境变量已设置argLine,直接替换会丢失原有参数。可通过@{argLine}继承原有配置,再追加所需参数:

    <argLine>@{argLine} --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.lang.reflect=ALL-UNNAMED --add-opens java.base/java.util.concurrent=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED</argLine>
    
  • 升级maven-surefire-plugin版本:3.1.2版本对JDK17的兼容性存在瑕疵,建议升级到最新稳定版(如3.2.5),新版本对模块系统的支持更完善:

    <version>3.2.5</version>
    
  • 验证fork模式参数传递:你设置了forkCount=10和reuseForks=false,确保Maven在fork新JVM时能正确传递argLine参数。若仍有问题,可临时将forkCount设为1测试,排除多fork进程的参数传递问题。

内容的提问来源于stack exchange,提问作者Jared

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 02:06:24