m0_68356026 2024-12-10 16:53 采纳率: 62.5%
浏览 807
已结题

启动单元测试时出现以下问题

出现的问题

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 (C:\CodeTool\Java\apache-maven-3.9.9\Local_repo\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


jdk版本

C:\Users\Max>java --version
openjdk 21.0.2 2024-01-16
OpenJDK Runtime Environment (build 21.0.2+13-58)
OpenJDK 64-Bit Server VM (build 21.0.2+13-58, mixed mode, sharing)

maven版本是3.99

springboot的java版本是21

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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.4.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.example</groupId>
    <artifactId>springboot_mybatis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot_mybatis</name>
    <description>springboot_mybatis</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>21</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>3.0.4</version>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter-test</artifactId>
            <version>3.0.4</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>


  • 写回答

1条回答 默认 最新

  • 趣享先生 优质创作者: 编程框架技术领域 2024-12-10 17:09
    关注

    该回答参考ChatGPT,由吾为汝整理提供如若有用,还请题主采纳!

    根据你提供的信息,出现的警告主要与 Mockito 的动态代理机制有关,特别是在使用 JDK 21 时。以下是一些解决方案和建议,帮助你解决这个问题:

    1. 添加 Mockito 作为 Java Agent

    根据警告信息,建议将 Mockito 作为 Java Agent 添加到你的 Maven 构建中。你可以在 pom.xml 中的 build 部分添加以下配置:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <argLine>-javaagent:${settings.localRepository}/org/mockito/mockito-core/5.4.0/mockito-core-5.4.0.jar</argLine>
                </configuration>
            </plugin>
            <!-- 其他插件配置 -->
        </plugins>
    </build>
    

    请确保将 mockito-core 的版本替换为你项目中实际使用的版本。

    2. 更新 Mockito 版本

    确保你使用的 Mockito 版本是最新的。可以在 pom.xml 中添加 Mockito 依赖:

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>5.4.0</version> <!-- 请根据需要选择最新版本 -->
        <scope>test</scope>
    </dependency>
    

    3. 使用 -XX:+EnableDynamicAgentLoading

    如果你在运行测试时仍然遇到问题,可以尝试在 JVM 参数中添加 -XX:+EnableDynamicAgentLoading。这可以在 maven-surefire-plugin 的配置中添加:

    <configuration>
        <argLine>-XX:+EnableDynamicAgentLoading</argLine>
    </configuration>
    

    4. 检查其他依赖

    确保你的其他依赖(如 Spring Boot 和 MyBatis)都是兼容 JDK 21 的版本。可以考虑更新到最新版本。

    5. 运行测试

    在完成上述更改后,尝试重新运行你的单元测试,看看问题是否解决。

    6. 参考 Mockito 文档

    最后,建议查看 Mockito 的官方文档,以获取更多关于如何配置和使用 Mockito 的信息。

    如果你在实施这些步骤时遇到任何问题,请告诉我,我们可以一步步来解决。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月22日
  • 已采纳回答 12月14日
  • 创建了问题 12月10日