单元测试 - SpringBoot2+Mockito实战
单元测试 - SpringBoot2+Mockito实战
在真实的开发中,我们通常是使用SpringBoot的,目前SpringBoot是v2.4.x的版本(SpringBoot 2.2.2.RELEASE之前默认是使用 JUnit4,之后版本默认使用Junit5);所以我们写个基于SpringBoot2.4+H2的内存库的简单例子,同时加点必要的单元测试。@pdai
SpringBoot对单测试的差异
SpringBoot 2.2.2.RELEASE之前默认是使用 JUnit4,之后版本默认使用Junit5。
pringboot+junit4:
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootQuickStartApplicationTests {
private MockMvc mvc;
@Before
public void setUp() throws Exception {
mvc = MockMvcBuilders.standaloneSetup(new UserController()).build();
}
@Test
public void contextLoads() throws Exception {
RequestBuilder request = null;
request = MockMvcRequestBuilders.get("/")
.contentType(MediaType.APPLICATION_JSON);
mvc.perform(request)
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
}
}
springboot+junit5:
@SpringBootTest
// 使用spring的测试框架
@ExtendWith(SpringExtension.class)
class SpringbootQuickStartApplicationTests {
private MockMvc mockMvc;
@BeforeEach // 类似于junit4的@Before
public void setUp() throws Exception {
mockMvc = MockMvcBuilders.standaloneSetup(new UserController()).build();
}
@Test
void contextLoads() throws Exception {
RequestBuilder request = null;
request = MockMvcRequestBuilders.get("/")
.contentType(MediaType.APPLICATION_JSON);
mockMvc.perform(request)
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
}
}
项目实践
Spring Boot 2.4.2 + H2 + Lombok + Spring Boot Test (默认包含了 Junit5 + Mockito)。
Demo程序准备
- 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.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>tech.pdai</groupId>
<artifactId>java-springboot-unit5</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>java-springboot-unit5</name>
<description>java-springboot-unit5</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</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>
</dependencies>
<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>
- application.yml
spring:
datasource:
platform: h2
driverClassName: org.h2.Driver
url: jdbc:h2:mem:testdb;MODE=MYSQL;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false
username: pdai
password: pdai
schema: classpath:db/schema/user-schema.sql
data: classpath:db/data/user-data.sql
initialization-mode: always
h2:
console:
settings:
trace: true
web-allow-others: true
enabled: true
path: /h2-console
jpa:
show-sql: true
hibernate:
ddl-auto: update
generate-ddl: false
open-in-view: false
- 数据库文件准备
schema
DROP TABLE IF EXISTS user;
CREATE TABLE user (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name varchar(35),
phone varchar(35)
);
data
insert into user(id,name,phone) values(1,'pdai','123456');
insert into user(id,name,phone) values(2,'zhangsan','123456');
- entity
package tech.pdai.springboot2unit5.entity;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.*;
/**
* User.
*/
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column
private String name;
@Column
private String phone;
}
- dao
package tech.pdai.springboot2unit5.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import tech.pdai.springboot2unit5.entity.User;
/**
* user dao.
*/
@Repository
public interface UserRepository extends JpaRepository<User, Integer> {
}
- service
package tech.pdai.springboot2unit5.service;
import tech.pdai.springboot2unit5.entity.User;
import java.util.List;
/**
* user service.
*/
public interface IUserService {
/**
* find all user.
*
* @return list
*/
List<User> findAll();
}
package tech.pdai.springboot2unit5.service.impl;
import org.springframework.stereotype.Service;
import tech.pdai.springboot2unit5.dao.UserRepository;
import tech.pdai.springboot2unit5.entity.User;
import tech.pdai.springboot2unit5.service.IUserService;
import java.util.List;
/**
* User service impl.
*/
@Service
public class UserServiceImpl implements IUserService {
/**
* user dao.
*/
private final UserRepository userRepository;
/**
* init.
*
* @param userRepository2 user dao
*/
public UserServiceImpl(final UserRepository userRepository2) {
this.userRepository = userRepository2;
}
/**
* find all user.
*
* @return list
*/
@Override
public List<User> findAll() {
return userRepository.findAll();
}
}
- Controller
package tech.pdai.springboot2unit5.controller;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.pdai.springboot2unit5.entity.User;
import tech.pdai.springboot2unit5.service.IUserService;
import java.util.List;
/**
* User controller.
*/
@RestController
public class UserController {
/**
* user service.
*/
private final IUserService userService;
/**
* init.
*
* @param userService2 user service
*/
public UserController(final IUserService userService2) {
this.userService = userService2;
}
/**
* find user list.
*
* @return list
*/
@GetMapping("user/list")
public ResponseEntity<List<User>> list() {
return ResponseEntity.ok(userService.findAll());
}
}
测试类
在实际的项目中可以使用profile来区分测试ut,使用test profile(包含H2内存库),真实环境使用MySQL或其它。
- main
package tech.pdai.springboot2unit5;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Profile;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import tech.pdai.springboot2unit5.service.IUserService;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
/**
* A way to test from H2.
* <p>
* Just a demo, and change profile to 'test' for H2, and 'product' for MySQL.
*/
@Profile("default")
@ExtendWith(SpringExtension.class)
@SpringBootTest
class JavaSpringbootUnit5ApplicationTests {
@Autowired
IUserService userService;
@Test
@DisplayName("Integration test")
void contextLoads() {
assertFalse(userService.findAll().isEmpty());
assertEquals("pdai", userService.findAll().get(0).getName());
}
}
- controller
mockMvc
package tech.pdai.springboot2unit5.controller;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import tech.pdai.springboot2unit5.entity.User;
import tech.pdai.springboot2unit5.service.IUserService;
import java.util.Collections;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* user controller test - use mockito.
*/
@ExtendWith(SpringExtension.class)
@WebMvcTest(value = UserController.class)
class UserControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private IUserService userService;
/**
* test find all user.
*
* @throws Exception exception
*/
@Test
@DisplayName("Test findAll()")
public void list() throws Exception {
Mockito.when(userService.findAll()).thenReturn(
Collections.singletonList(new User(1, "pdai.tech", "1221111")));
mockMvc.perform(MockMvcRequestBuilders.get("/user/list")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
verify(userService, times(1)).findAll();
}
}
- service
package tech.pdai.springboot2unit5.service.impl;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import tech.pdai.springboot2unit5.entity.User;
import java.util.Collections;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@ExtendWith(SpringExtension.class)
class UserServiceImplTest {
@Mock
private UserServiceImpl userService;
@Test
public void findAll() {
//Given
Mockito.when(userService.findAll()).thenReturn(
Collections.singletonList(new User(1, "pdai.tech", "1221111")));
//When
List<User> userDtoList = userService.findAll();
//Then
assertFalse(userDtoList.isEmpty());
verify(userService, times(1)).findAll();
}
}
- 测试结果
Util测试
如果包含静态util的测试还可以加PowerMokito.
具体可以参考:测试结合powermock支持静态方法
单元测试 - SpringBoot2+Mockito实战的更多相关文章
- 单元测试系列:Mock工具之Mockito实战
更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6780719.html 在实际项目中写单 ...
- 单元测试系列之五:Mock工具之Mockito实战
更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6780719.html 在实际项目中写单 ...
- 单元测试利器Mockito框架
什么是Mock Mock 的中文译为仿制的,模拟的,虚假的.对于测试框架来说,即构造出一个模拟/虚假的对象,使我们的测试能顺利进行下去. Mock 测试就是在测试过程中,对于某些 不容易构造(如 Ht ...
- 单元测试与Mockito
1.什么是单元测试? 顾名思义单元测试就是对软件系统中最小的单元(函数.类)做测试,类似焊接电路板前对每个电容器(电子元器件)的测试.从软件测试分级来看,单元测试是最底层也是离程序员最近的一层,一般由 ...
- Spring Boot 单元测试详解+实战教程
Spring Boot 的测试类库 Spring Boot 提供了许多实用工具和注解来帮助测试应用程序,主要包括以下两个模块. spring-boot-test:支持测试的核心内容. spring-b ...
- 单元测试神器Mockito
Mockit是一种mock工具/框架.mock可以模拟各种各样的对象,从而代替真正的对象做出希望的响应 1.工程中引入Mockito #以gradle的方式为例 testCompile("o ...
- 单元测试系列之二:Mock工具Jmockit实战
更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6760272.html Mock工具Jm ...
- 单元测试系列:JUnit单元测试规范
更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6762032.html Junit测试代 ...
- 单元测试系列:Mock工具Jmockit使用介绍
更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6760272.html Mock工具Jm ...
随机推荐
- Spring Cloud实战 | 第十一篇:Spring Cloud Gateway 网关实现对RESTful接口权限控制和按钮权限控制
一. 前言 hi,大家好,这应该是农历年前的关于开源项目 的最后一篇文章了. 有来商城 是基于 Spring Cloud OAuth2 + Spring Cloud Gateway + JWT实现的统 ...
- [Golang]-1 Slice与数组的区别
目录 数组 1.创建数组: 2.数组是值拷贝传递: 切片(slice) 1.首先看看slice的源码结构: 2.slice的创建: 3.slice使用make创建 4.切片作为参数传递 5.Golan ...
- Zabbix 监控网站
官网教学步骤 配置 Web 监控 创建 Web 场景 配置 Web 场景 配置 Web 监控步骤 一共划分为 5 个步骤: 监测访问登录页面 模拟登录功能 # sid 变量的值 regex:name= ...
- Commons Collections2分析
0x01.POC分析 //创建一个CtClass对象的容器 ClassPool classPool=ClassPool.getDefault(); //添加AbstractTranslet的搜索路径 ...
- 探索 .NET Core 依赖注入的 IServiceCollection
如果您使用了.NET Core,则很可能已使用Microsoft.Extensions.DependencyInjection中的内置依赖项注入容器,在本文中,我想更深入地了解Microsoft De ...
- 机器学习入门实战——基于knn的airbnb房租预测
数据读取 import pandas as pd features=['accommodates','bathrooms','bedrooms','beds','price','minimum_nig ...
- css position sticky All In One
css position sticky All In One css sticky & 吸顶效果 demo https://codepen.io/xgqfrms/pen/PoqyVYz ref ...
- Set-Cookie & Secure & HttpOnly & SameSite
Set-Cookie & Secure & HttpOnly & SameSite HTTP/Headers/Set-Cookie Set-Cookie https://dev ...
- cnblogs 日期错乱 bug
cnblogs 日期错乱 bug 时间错乱 bug archive/2004/01/13/ 什么鬼 呀默认时间戳 https://www.cnblogs.com/xgqfrms/archive/200 ...
- 如何用 js 实现一个 call 函数
如何用 js 实现一个 call 函数 原理 实现方式 总结 refs https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc ...