基于SpringBoot Mock单元测试详解
作者:紫蝶侠 时间:2021-09-25 02:49:41
Junit中的基本注解:
@Test
:使用该注解标注的public void方法会表示为一个测试方法;@BeforeClass
:表示在类中的任意public static void方法执行之前执行;@AfterClass
:表示在类中的任意public static void方法之后执行;@Before
:表示在任意使用@Test注解标注的public void方法执行之前执行;@After
:表示在任意使用@Test注解标注的public void方法执行之后执行;
SpringBoot 单元测试详解(Mockito、MockBean)
SpringBoot 单元测试(cobertura 生成覆盖率报告)
1.Mock的概念:
所谓的mock就是创建一个类的虚假的对象,在测试环境中,用来替换掉真实的对象,以达到两大目的:
验证这个对象的某些方法的调用情况,调用了多少次,参数是什么等等指定这个对象的某些方法的行为,返回特定的值,或者是执行特定的动作 2. 添加依赖
新建的springBoot项目中默认包含了spring-boot-starter-test的依赖,如果没有包含可自行在pom.xml中添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
进入 spring-boot-starter-test-2.2.2.RELEASE.pom 可以看到该依赖中已经有单元测试所需的大部分依赖,如:
junit
mockito
hamcrest
注意包含的junit为junit5 ,在主要还是使用junit4所以在pom.xml中添加依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
这里如果不添加的话,在使用@RunWith注解的时候也会提示你添加,点击Add ‘JUnit4' to classpath也会自动在pom.xml帮你添加
若为非springboot项目,其他 spring 项目,需要自己添加 Junit 和 mockito 的依赖。SpringBoot不要添加,添加后Test的时候会出错
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
3. 常用的 Mockito 方法
Mockito的使用,一般有以下几种组合:
do/when
:包括doThrow(…).when(…)/doReturn(…).when(…)/doAnswer(…).when(…)given/will
:包括given(…).willReturn(…)/given(…).willAnswer(…)when/then
: 包括when(…).thenReturn(…)/when(…).thenAnswer(…)
例如:
given(userRepository.findByUserName(Mockito.anyString())).willReturn(user);
given + willReturn
given用于对指定方法进行返回值的定制,它需要与will开头的方法一起使用
通过willReturn可以直接指定打桩的方法的返回值
when(userRepository.findByUserName(Mockito.anyString())).thenReturn(user);
when + thenReturn
when的作用与Given有点类似,但它一般与then开头的方法一起使用。
thenReturn与willReturn类似,不过它一般与when一起使用。
来源:https://blog.csdn.net/yangshengwei230612/article/details/104753603
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
c#实现断点续传功能示例分享
Android开发工程中集成mob短信验证码功能的方法
![](https://img.aspxhome.com/file/2023/7/106807_0s.png)
springboot整合mybatis将sql打印到日志的实例详解
教你轻松制作java音乐播放器
![](https://img.aspxhome.com/file/2023/0/91670_0s.jpg)
详解java动态代理模式
![](https://img.aspxhome.com/file/2023/0/71870_0s.png)
spring boot devtools在Idea中实现热部署方法
第一次编写Java流布局图形界面
![](https://img.aspxhome.com/file/2023/5/71865_0s.jpg)
c#编写的番茄钟倒计时器代码
Java实现商城订单超时取消功能
Vs2022环境下安装低版本.net framework的实现步骤
![](https://img.aspxhome.com/file/2023/8/78988_0s.png)
IDEA高效使用设置指南
![](https://img.aspxhome.com/file/2023/7/108377_0s.jpg)
C# 获取当前总毫秒数的实例讲解
Java输入年份和月份判断多少天实例代码
Java详细分析String类与StringBuffer和StringBuilder的使用方法
![](https://img.aspxhome.com/file/2023/6/64946_0s.png)
monkeyrunner之电脑安装驱动(5)
![](https://img.aspxhome.com/file/2023/4/87334_0s.png)
Android实现网易云音乐高仿版流程
![](https://img.aspxhome.com/file/2023/2/92622_0s.png)
Jenkins+Git+Maven自动化部署配置详解
![](https://img.aspxhome.com/file/2023/4/106194_0s.jpg)
解决Spring或SpringBoot开启事务以后无法返回自增主键的问题
Java读取Map的两种方法与对比
![](https://img.aspxhome.com/file/2023/3/65213_0s.png)