基于springboot的工程,正常单元测试,可以利用IDEA的goto功能自动生成对应的测试类(测试方法),然后在生成的测试类加注解@SpringBootTest,执行对应的test方法即可。但是这样默认是会启动整个springboot应用的,如果有web,还会启动web容器。这个时间比较久,不够优雅。
Mapper层的单测
直接撸代码~文章来源:https://www.toymoban.com/news/detail-508706.html
1、test类
package com.test.junit.mapper;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import com.test.junit.entity.UserInfo;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.CollectionUtils;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.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")
class IUserInfoMapperTest {
@Autowired
private IUserInfoMapper userInfoMapper;
@Test
public void testSelectUser(){
Map<String, Object> map = new HashMap<>();
map.put("name", "asdfa");
List<UserInfo> userInfos = userInfoMapper.selectByMap(map);
Assertions.assertTrue(!CollectionUtils.isEmpty(userInfos));
}
}
2、启动配置文件
对应的application.yml配置文件:文章来源地址https://www.toymoban.com/news/detail-508706.html
spring:
datasource:
url: jdbc:mysql://localhost:3306/my_test?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8
username: root
password: 123456
3、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>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>
到了这里,关于Springboot优雅单元测试之mapper的测试(基于mybatis-plus)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!