0


Springboot优雅单元测试之mapper的测试(基于mybatis-plus)

Springboot优雅单元测试-mapper的测试(基于mybatis-plus)

基于springboot的工程,正常单元测试,可以利用IDEA的goto功能自动生成对应的测试类(测试方法),然后在生成的测试类加注解@SpringBootTest,执行对应的test方法即可。但是这样默认是会启动整个springboot应用的,如果有web,还会启动web容器。这个时间比较久,不够优雅

Mapper层的单测

直接撸代码~

1、test类

packagecom.test.junit.mapper;importcom.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;importcom.test.junit.entity.UserInfo;importorg.apache.ibatis.session.SqlSessionFactory;importorg.junit.jupiter.api.Assertions;importorg.junit.jupiter.api.Test;importorg.mybatis.spring.annotation.MapperScan;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.util.CollectionUtils;importjavax.sql.DataSource;importjava.util.HashMap;importjava.util.List;importjava.util.Map;/**
主要是这里的代码:
@SpringBootTest注解里面的classes可以定义只想加载的bean类,
比如此处只需要测试mybatis-plus的mapper接口,那么需要用到的类配置进来即可,
这样就不会加载controller层、service层等没用到的资源。
*/@SpringBootTest(
        classes ={IUserInfoMapper.class,DataSourceAutoConfiguration.class,MybatisPlusAutoConfiguration.class,DataSource.class,SqlSessionFactory.class})@MapperScan(basePackages ="com.test.junit.mapper")classIUserInfoMapperTest{@AutowiredprivateIUserInfoMapper userInfoMapper;@TestpublicvoidtestSelectUser(){Map<String,Object> map =newHashMap<>();
        map.put("name","asdfa");List<UserInfo> userInfos = userInfoMapper.selectByMap(map);Assertions.assertTrue(!CollectionUtils.isEmpty(userInfos));}}

2、启动配置文件

对应的application.yml配置文件:

spring:datasource:url: jdbc:mysql://localhost:3306/my_test?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8username: root
    password:123456

3、pom.xml依赖

<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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>2.7.2</version><relativePath/><!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>rank-demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</name><description>Demo project for Spring Boot</description><properties><java.version>11</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.1</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>com.github.jsonzou</groupId><artifactId>jmockdata</artifactId><version>4.3.0</version><scope>compile</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.assertj</groupId><artifactId>assertj-core</artifactId><version>3.22.0</version><scope>test</scope></dependency><dependency><groupId>org.mockito</groupId><artifactId>mockito-core</artifactId><version>4.3.1</version><scope>test</scope></dependency><dependency><groupId>org.mockito</groupId><artifactId>mockito-inline</artifactId><version>4.3.1</version><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.11</version></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-core</artifactId><version>1.2.11</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.36</version></dependency></dependencies></dependencyManagement><build><plugins><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>

本文转载自: https://blog.csdn.net/xiaocy66/article/details/126716098
版权归原作者 小月施主 所有, 如有侵权,请联系我们删除。

“Springboot优雅单元测试之mapper的测试(基于mybatis-plus)”的评论:

还没有评论