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

Spring Boot测试配置Mockito Agent后仍出现自附加警告问题

Spring Boot测试中Mockito自附加警告未解决问题

遇到的警告信息

Mockito is currently self-attaching to enable the inline-mock-maker. This will no longer work in future releases of the JDK. Please add Mockito as an agent to your build what is described in Mockito's documentation: https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#0.3
WARNING: A Java agent has been loaded dynamically (/Users/lcnicolau/.m2/repository/net/bytebuddy/byte-buddy-agent/1.15.10/byte-buddy-agent-1.15.10.jar)
WARNING: If a serviceability tool is in use, please run with -XX:+EnableDynamicAgentLoading to hide this warning
WARNING: If a serviceability tool is not in use, please run with -Djdk.instrument.traceUsage for more information
WARNING: Dynamic loading of agents will be disallowed by default in a future release
OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended

已尝试的官方配置

按照官方文档要求添加了以下Maven插件配置,但警告仍然存在:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>properties</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <argLine>@{argLine} -javaagent:${org.mockito:mockito-core:jar}</argLine>
    </configuration>
</plugin>

当前环境版本

  • Spring Boot 3.4.0
  • Apache Maven 3.9.9
  • OpenJDK 21.0.4

解决方案建议

1. 确保依赖变量正确解析

maven-dependency-plugin的properties目标需要在surefire插件执行前运行,确保${org.mockito:mockito-core:jar}变量被正确替换为实际Jar路径。可以给依赖插件的执行添加明确ID并绑定到测试阶段之前:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>resolve-mockito-jar</id>
            <phase>process-test-classes</phase>
            <goals>
                <goal>properties</goal>
            </goals>
        </execution>
    </executions>
</plugin>

2. 修正Surefire插件配置

移除@{argLine}(如果没有其他需要继承的JVM参数),或者确保Surefire插件的配置优先级正确:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <argLine>-javaagent:${org.mockito:mockito-core:jar}</argLine>
    </configuration>
    <executions>
        <execution>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <argLine>-javaagent:${org.mockito:mockito-core:jar}</argLine>
            </configuration>
        </execution>
    </executions>
</plugin>

3. 适配Spring Boot插件配置

如果项目使用spring-boot-maven-plugin,可以直接在该插件中配置JVM参数,避免Surefire配置被覆盖:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <jvmArguments>-javaagent:${org.mockito:mockito-core:jar}</jvmArguments>
    </configuration>
</plugin>

4. IDE运行配置适配

如果在IDE中直接运行测试,以IDEA为例,勾选「Delegate IDE build/run actions to Maven」,确保IDE使用Maven的JVM参数配置。或者手动在测试运行配置的VM选项中添加-javaagent:/path/to/mockito-core-x.x.x.jar。

5. 清理Maven缓存

执行mvn clean install -U清理本地缓存并重新下载依赖,确保变量解析没有缓存问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 15:04:53