【错误解决】Mockito在JDK 22的兼容性问题

前言:在学习SpringBoot整合MyBatisPlus时,在启动类里添加的@MapperScan注解扫描项目的mapper文件夹相关类的方法,测试时一直出错失败,这样的话学习mybatisplusCRUD的操作也没办法测试运行,我在网上检索相关问题的比较新的参考很少,也和我遇到的问题重叠率不高。

相关错误信息:Caused by: [CIRCULAR REFERENCE: java.lang.IllegalStateException: Internal problem occurred, please report it. Mockito is unable to load the default implementation of class that is a part of Mockito distribution. Failed to load interface org.mockito.plugins.MockMaker]

相关说明:本文项目JDK版本为22。由于Mockito在JDK 22中无法动态加载Java Agent来启用inline-mock-maker功能,Mockito的inline-mock-maker依赖于Java的Agent机制,而JDK 22默认情况下不再支持动态加载Agent,这导致了java.lang.IllegalStateException异常。

解决方法在文末,有类似问题的小伙伴可以试一试该解决方法。接下来我完整记录一下这个错误的初始案例(该案例是一个完整的小案例,可以拿去创建并测试):

创建SpringBoot项目,添加MyBatisPlus起步依赖和相关依赖:

    
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.5.5
        

        
        
            mysql
            mysql-connector-java
            8.0.32
        

        
        
            org.projectlombok
            lombok
            1.18.36
        
    

创建SpringBoot配置文件application.yml添加配置数据源:

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql:///school?serverTimezone=UTC
    username: root
    password: root

前情提要:数据库这里是在localhost下创建一个名为school的数据库,在school中创建一个名为student的表。在Mysql中准备数据:

CREATE TABLE `student` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(255) DEFAULT NULL,
 `email` varchar(255) DEFAULT NULL,
    `gender` varchar(255) DEFAULT NULL,
 `age` int(11) DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

        自己插入几条数据用于后面的测试。

编写一个项目实体类Student:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
  private Integer id;
  private String name;
  private String email;
  private String gender;
  private Integer age;
}

编写StudentMapper接口,继承BaseMapper

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.util.List;


public interface StudentMapper extends BaseMapper {
    List selectStudentByName(String studentName);
}

在SpringBoot启动类中添加 @MapperScan 注解扫描mapper文件夹,@MapperScan 注解中的路径写你的mapper路径!:

@SpringBootApplication
@MapperScan("com.your.path.mapper")
public class SpringbootMpApplication {
  public static void main(String[] args) {
    SpringApplication.run(SpringbootMpApplication.class, args);
   }
}

测试:在项目的test文件下创建对应的包,然后创建StudentMapperTest方法进行测试:

@SpringBootTest
public class StudentMapperTest {
  @Autowired
  private StudentMapper studentMapper;

  @Test
  public void testFindById(){
    Student student = studentMapper.selectById(1);
    System.out.println(student);
   }
}

在编写完这个小案例后,发现运行出错,我尝试一番后解决办法如下:

        在项目配置中添加一些依赖:

        
            com.baomidou
            mybatis-plus
            3.5.5
        
        
            org.mybatis
            mybatis
            3.5.15
        
        
            org.mybatis
            mybatis-spring
            3.0.4
        
        
            org.mockito
            mockito-subclass
            5.12.0
        

我在项目中添加这些依赖后测试正常。注:添加的mybatis的两个依赖可能对SpringBoot整合MyBatisPlus项目本体没有作用,不影响的情况下是可以加上的。

你可能感兴趣的:(spring,boot,mybatis,plus,Mockito,mybatis)