springboot-web进阶(四)——单元测试
一、概述
基础知识,参考:https://www.cnblogs.com/ysw-go/p/5447056.html
二、springboot的单元测试
1.入门测试类
最重要的不要忘记类上面的依赖,以及类里面方法上的@Test(底层是jUnit)
package com.example.demo; import com.example.demo.service.GirlService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; /**
* GirlService测试类
*
* @author zcc ON 2018/2/9
**/
@RunWith(SpringRunner.class)
@SpringBootTest
public class GirlServiceTest { @Autowired
private GirlService girlService;
@Test
public void findOneTest() {
Assert.assertEquals(new Integer(20), girlService.findOne(4).getAge());
}
}
这样,就可以看到相关结果了:

// 为了高大上一点,请不要再使用小白式的sout了,多使用断言.
2.使用IDEA自动生成测试类
例如还是测试上面的service里的findOne,则通过在方法上右击->Goto->Test

3.controller的API单元测试
同样,在方法上右击,Goto->Test,得到测试类
package com.example.demo.controller; import com.example.demo.SpringbootDemoApplicationTests;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import static org.junit.Assert.*; @AutoConfigureMockMvc
public class GirlControllerTest extends SpringbootDemoApplicationTests { @Autowired
private MockMvc mvc;
@Test
public void getList() throws Exception {
// 测试状态码
mvc.perform(MockMvcRequestBuilders.get("/girls"))
.andExpect(MockMvcResultMatchers.status().isOk());
} }
使用单元测试还有一个用处是在打包是会自动跑单元测试,并会给出测试结果,失败时将会报ERROR!
还有其他测试选项,例如测试.content.string("abc"来测试返回内容),完整的API,参考:这里
springboot-web进阶(四)——单元测试的更多相关文章
- springboot web项目的单元测试
不废话,直接上代码. //// SpringJUnit支持,由此引入Spring-Test框架支持! @RunWith(SpringJUnit4ClassRunner.class) //// 指定我们 ...
- SpringBootTest单元测试实战、SpringBoot测试进阶高级篇之MockMvc讲解
1.@SpringBootTest单元测试实战 简介:讲解SpringBoot的单元测试 1.引入相关依赖 <!--springboot程序测试依赖,如果是自动创建项目默认添加--> &l ...
- Springboot系列(四)web静态资源配置详解
Springboot系列(四)web静态资源配置 往期精彩 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 SpringBoot系列(三)配 ...
- 网站开发进阶(四十四)input type="submit" 和"button"的区别
网站开发进阶(四十四)input type="submit" 和"button"的区别 在一个页面上画一个按钮,有四种办法: 这就是一个按钮.如果你不写ja ...
- 网站开发进阶(四十三)html中,路径前加“/” 与不加“/”的区别
网站开发进阶(四十三)html中,路径前加"/" 与不加"/"的区别 前言 <script src="js/downloadify.js&quo ...
- springboot+web文件上传和下载
一.首先安装mysql数据库,开启web服务器. 二.pom.xml文件依赖包配置如下: <?xml version="1.0" encoding="UTF-8&q ...
- SpringBoot Web开发(5) 开发页面国际化+登录拦截
SpringBoot Web开发(5) 开发页面国际化+登录拦截 一.页面国际化 页面国际化目的:根据浏览器语言设置的信息对页面信息进行切换,或者用户点击链接自行对页面语言信息进行切换. **效果演示 ...
- IDEA中SpringBoot项目快速创建单元测试
如何在IDEA中对于SpringBoot项目快速创建单元测试 创建测试用例 右键需要进行测试的方法,选择GO TO然后选择Test 点击Create New Test 勾选需要创建单元测试的方法 然后 ...
- Python爬虫进阶四之PySpider的用法
审时度势 PySpider 是一个我个人认为非常方便并且功能强大的爬虫框架,支持多线程爬取.JS动态解析,提供了可操作界面.出错重试.定时爬取等等的功能,使用非常人性化. 本篇内容通过跟我做一个好玩的 ...
- SpringBoot Web项目中中如何使用Junit
Junit这种老技术,现在又拿出来说,不为别的,某种程度上来说,更是为了要说明它在项目中的重要性. 凭本人的感觉和经验来说,在项目中完全按标准都写Junit用例覆盖大部分业务代码的,应该不会超过一半. ...
随机推荐
- 离线安装SharePoint2016
离线安装SharePoint2016的过程中,遇到了不少问题,该文章将安装过程尽量详细描述,供自己后续参考,请不要嫌文章啰嗦哈. 本人使用的是Windows Server 2012 R2 Standa ...
- 使用 NGINX 进行微程序缓存的好处
[编者按]本文作者为 Owen Garrett,主要介绍使用 nginx 进行微程序缓存的好处,辅之以生动的实例.文章系国内 ITOM 管理平台 OneAPM 编译呈现. NGINX 和 NGINX ...
- Ehcache.xml 配置及属性说明
1.配置样例 <?xml version="1.0" encoding="UTF-8"?> <ehcache> <diskStor ...
- Oracle 数据库执行慢SQL
) hou, - ))) mini, c.sql_address, c.inst_id,f.full_name,u.user_name, b.user_concurrent_program_name, ...
- SqlServer查询存储过程中包含指定的内容
存储过程太多,有时只记得存储过程里面的某一点类容,想要找到对应的存储过程: SELECT obj.Name 存储过程名, sc.TEXT 存储过程内容 FROM syscomments sc INNE ...
- Tomcat 安全设置 及 内存修改
1.删除%tomcatRoot%/webapps目录下的examples.docs文件夹 2.修改%tomcatRoot%/conf/tomcat-users.xml <?xml version ...
- 使用xtrabackup不停服务做从库
一.安装xtrabackup 1.YUM安装,下载percona源: yum install http://www.percona.com/downloads/percona-release/redh ...
- [翻译] InstagramPhotoPicker
InstagramPhotoPicker Present Image Picker like Instagram. 展示图片选择器,像Instagram这款应用一样. Installation - 安 ...
- 有序字典(OrderedDict)、默认字典(defaultdict)内置函数
http://www.cnblogs.com/wupeiqi/articles/5115190.html import collections do = collections.OrderedDict ...
- Python学习---IO的异步[asyncio +aiohttp模块]
aiohttp aiohttp是在asyncio模块基础上封装的一个支持HTTP请求的模块,内容比8.4.2[基于asyncio实现利用TCP模拟HTTP请求]更全面 安装aiohttp: pip3 ...