Spring Boot(十二)单元测试JUnit
一、介绍
JUnit是一款优秀的开源Java单元测试框架,也是目前使用率最高最流行的测试框架,开发工具Eclipse和IDEA对JUnit都有很好的支持,JUnit主要用于白盒测试和回归测试。
- 白盒测试:把测试对象看作一个打开的盒子,程序内部的逻辑结构和其他信息对测试人
员是公开的; - 回归测试:软件或环境修复或更正后的再测试;
- 单元测试:最小粒度的测试,以测试某个功能或代码块。一般由程序员来做,因为它需要知道内部程序设计和编码的细节;
JUnit GitHub地址:https://github.com/junit-team
二、JUnit使用
开发环境:
- Spring Boot 2.0.4 RELEASE
- JUnit 4.12
- Maven
- IDEA 2018.2
2.1 检测JUnit依赖
如果是Spring Boot项目默认已经加入了JUnit框架支持,可在pom.xml中查看:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
如果Maven项目中没有添加JUnit依赖,可参照如上代码,手动添加。
2.2 基础使用
简单的测试代码如下:
@RunWith(SpringRunner.class)
@SpringBootTest
public class SimpleTest {
@Test
public void doTest() {
int num = new Integer(1);
Assert.assertEquals(num, 1);
}
}
在测试类中邮件运行项目,效果如下:

从控制台可以看出测试通过了。
2.3 注解说明
2.3.1 注解列表
- @RunWith:标识为JUnit的运行环境;
- @SpringBootTest:获取启动类、加载配置,确定装载Spring Boot;
- @Test:声明需要测试的方法;
- @BeforeClass:针对所有测试,只执行一次,且必须为static void;
- @AfterClass:针对所有测试,只执行一次,且必须为static void;
- @Before:每个测试方法前都会执行的方法;
- @After:每个测试方法前都会执行的方法;
- @Ignore:忽略方法;
2.3.2 超时测试
代码如下,给Test设置timeout属性即可,时间单位为毫秒:
@Test(timeout = 1000)
2.4 断言测试
断言测试也就是期望值测试,是单元测试的核心也就是决定测试结果的表达式,Assert对象中的断言方法:
- Assert.assertEquals 对比两个值相等
- Assert.assertNotEquals 对比两个值不相等
- Assert.assertSame 对比两个对象的引用相等
- Assert.assertArrayEquals 对比两个数组相等
- Assert.assertTrue 验证返回是否为真
- Assert.assertFlase 验证返回是否为假
- Assert.assertNull 验证null
- Assert.assertNotNull 验证非null
代码示例如下:
@Test
public void doTest() {
String[] string1 = {"1", "2"};
String[] string2 = string1;
String[] string3 = {"1", "2"};
Assert.assertEquals(string1, string2);
Assert.assertEquals(string2, string3);
Assert.assertSame(string1, string2);
Assert.assertSame(string2, string3); //验证不通过,string2、string3指向的引用不同
}
2.5 Web模拟测试
在Spring Boot项目里面可以直接使用JUnit对web项目进行测试,Spring 提供了“TestRestTemplate”对象,使用这个对象可以很方便的进行模拟请求。
Web测试只需要进行两步操作:
- 在@SpringBootTest注解上设置“ebEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT”随机端口;
- 使用TestRestTemplate进行post或get请求;
示例代码如下:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class UserControllerTest {
@Autowired
private TestRestTemplate restTemplate;
@Test
public void getName() {
String name = restTemplate.getForObject("/name", String.class);
System.out.println(name);
Assert.assertEquals("Adam", name);
}
}
其中getForObject的含义代表执行get请求,并返回Object结果,第二个参数设置返回结果为String类型,更多的请求方法:
- getForEntity:Get请求,返回实体对象(可以是集合);
- postForEntity:Post请求,返回实体对象(可以是集合);
- postForObject:Post请求,返回对象;
2.6 数据库测试
在测试数据操作的时候,我们不想让测试污染数据库,也是可以实现的,只需要添加给测试类上添加“@Transactional”即可,这样既可以测试数据操作方法,又不会污染数据库了。
示例代码如下:
@Test
@Transactional
public void saveTest() {
User user = new User();
user.setName("Adam");
user.setAge(19);
user.setPwd("123456");
userRepository.save(user);
System.out.println("userId:" + user.getId());
Assert.assertTrue(user.getId()>0);
}
执行效果如下:

我们可以看到Id有了,也测试通过了,说明数据是添加是正常的,但查看数据库发现数据里面是没有这条数据的。
如果把“@Transactional”去掉的话,数据库就会正常插入了。
2.7 Idea快速开启测试
在Idea里面可以快速的添加测试的方法,只需要在要测试的类里面右键选择“GoTo”点击“Test”,选择你需要测试的代码,点击生成即可,如果是Windows 用户可以使用默认快捷键“Ctrl + Shift + T”,效果如下图:


选完方法之后,点击OK按钮,就生成了对应的测试代码,用户只需要完善框架里面的具体测试逻辑就可以了。
Spring Boot(十二)单元测试JUnit的更多相关文章
- Spring Boot(十二):spring boot如何测试打包部署
Spring Boot(十二):spring boot如何测试打包部署 一.开发阶段 1,单元测试 在开发阶段的时候最重要的是单元测试了,springboot对单元测试的支持已经很完善了. (1)在p ...
- (转)Spring Boot(十二):Spring Boot 如何测试打包部署
http://www.ityouknow.com/springboot/2017/05/09/spring-boot-deploy.html 有很多网友会时不时的问我, Spring Boot 项目如 ...
- spring boot(十二)打包部署
有很多网友会时不时的问我,spring boot项目如何测试,如何部署,在生产中有什么好的部署方案吗?这篇文章就来介绍一下spring boot 如何开发.调试.打包到最后的投产上线. 开发阶段 单元 ...
- Spring Boot(十二):Spring Boot 如何测试打包部署
有很多网友会时不时的问我, Spring Boot 项目如何测试,如何部署,在生产中有什么好的部署方案吗?这篇文章就来介绍一下 Spring Boot 如何开发.调试.打包到最后的投产上线. 开发阶段 ...
- spring boot / cloud (二十) 相同服务,发布不同版本,支撑并行的业务需求
spring boot / cloud (二十) 相同服务,发布不同版本,支撑并行的业务需求 有半年多没有更新了,按照常规剧本,应该会说项目很忙,工作很忙,没空更新,吧啦吧啦,相关的话吧, 但是细想想 ...
- Spring Boot(十四):spring boot整合shiro-登录认证和权限管理
Spring Boot(十四):spring boot整合shiro-登录认证和权限管理 使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公司都会涉 ...
- spring Boot(十九):使用Spring Boot Actuator监控应用
spring Boot(十九):使用Spring Boot Actuator监控应用 微服务的特点决定了功能模块的部署是分布式的,大部分功能模块都是运行在不同的机器上,彼此通过服务调用进行交互,前后台 ...
- Spring Boot(十八):使用Spring Boot集成FastDFS
Spring Boot(十八):使用Spring Boot集成FastDFS 环境:Spring Boot最新版本1.5.9.jdk使用1.8.tomcat8.0 功能:使用Spring Boot将文 ...
- 【spring boot】10.spring boot下的单元测试
spring boot下的单元测试,思前想后还是需要单独用一章篇幅来看看. 然后在看了介绍和使用时候,我感觉并不想多去看了. 但是还是给后来人留下参考的路径: 官网说明:https://spring. ...
- Spring Boot实战之单元测试
Spring Boot实战之单元测试 本文介绍使用Spring测试框架提供的MockMvc对象,对Restful API进行单元测试 Spring测试框架提供MockMvc对象,可以在不需要客户端-服 ...
随机推荐
- 通过命令行操作MYSQL的方法 以及导入大的SQL备份文件
运行 输入CMD 进入 命令行窗口 输入Mysql.exe的路径 如:c:/wamp/bin/mysql.exe 回车 这时出现 welcome to the mysql ...的提示 进入成 ...
- ES6学习
一.ES6的特点 1.let(变量),const(常量) 2.在ES6中不能重复定义 3.块级作用域 普通作用域 if(true){ var test =1; } console.log(test); ...
- dedecms给图片加水印覆盖整张图片
位置: /include/image.class.php $wmwidth = $imagewidth - $logowidth; $wmheight = $imageheight - $logohe ...
- java枚举使用 总结
补充几点: 1.枚举对象是可以用 == 比较. 2. TestEnum3反编译结果: F:\tree\Test\src\test>javap TestEnum3* Compiled from & ...
- TCPDF解决保存中文文件名的方法
PHP使用TCPDF生成PDF文件时,如果文件名是中文会被直接过滤掉,以下是TCPDF不能保存中文文件名的解决方法: 打开tcpdf.php文件,找到output函数,大约在8467行 或(7554) ...
- DOS命令(二)
1. findstr “要查找的字符串” 文件,用来从文件中检索包含相关内容的字符串集合. [例如:查找包含“TTL”的字符串] 2. del 要删除的文件,用来删除某个文件. 3. pause,用 ...
- 5 个免费的受欢迎的 SQLite 管理工具【申明:来源于网络】
5 个免费的受欢迎的 SQLite 管理工具 包含内容: SQLite Expert – Personal Edition SQLite Expert 提供两个版本,分别是个人版和专业版.其中个人版是 ...
- 美图App的移动端DNS优化实践:HTTPS请求耗时减小近半
本文引用了颜向群发表于高可用架构公众号上的文章<聊聊HTTPS环境DNS优化:美图App请求耗时节约近半案例>的部分内容,感谢原作者. 1.引言 移动互联网时代,APP 厂商之间的竞争非常 ...
- [Swift]LeetCode217. 存在重复元素 | Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- [Swift]LeetCode343. 整数拆分 | Integer Break
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...