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用例覆盖大部分业务代码的,应该不会超过一半. ...
随机推荐
- js判断字符串出现的次数
// 判断substr字符串在str中出现的次数 isIgnore是否忽略大小写! function countSubstr(str, substr, isIgnore) { var count; v ...
- linux上设置mysql编码
linux下设置mysql编码 linux下设置mysql编码 首先查找MySql的cnf文件的位置: [root@flyHome gaoxiang]# find / -iname '*.cnf' - ...
- Oracle EBS AR 更新客户
DECLARE l_return_status ); l_msg_count NUMBER; l_msg_data ); l_rec_type hz ...
- Oracle案例10——HWM(高水位线)性能优化
最近BI同事反馈说一张表的数据查询非常慢,这个表数据总共不到1W行数据,这么一说我们首先想到的是高水位带来的性能问题,即高水位线下占用过多数据块,而这些数据块其实是部分数据占用,大多数是空闲的数据块. ...
- FileStream对文本进行读写操作
class FileHelper { /// <summary> /// 检验文件路径是否合法 /// </summary> /// <param name=" ...
- [翻译] SACalendar
SACalendar 效果图: Introducing SACalendar - Easy to use and customizable iOS 7 Calendar SACalendar - 使用 ...
- Linux crontab命令详解
crontab:定时任务的守护进程,精确到分,设计秒的我们一般写脚本 -->相当于闹钟 日志文件: ll /var/log/cron* 编辑文件: vim /et ...
- gradle结合spring-boot生成可运行jar包,并打印日志
1.用gradle把springboot项目打包成jar 1.1 build.gradle 中添加 buildscript { repositories { mavenLocal() maven { ...
- 网络编程--Socket(套接字)
网络编程 网络编程的目的就是指直接或间接地通过网络协议与其他计算机进行通讯.网络编程中 有两个主要的问题,一个是如何准确的定位网络上一台或多台主机,另一个就是找到主机后 如何可靠高效的进行数据传输.在 ...
- ZT Android4.2蓝牙基础架构学习
Android4.2蓝牙基础架构学习 分类: Jellybean Bluetooth Bluetooth 2013-10-13 23:58 863人阅读 评论(3) 收藏 举报 androidblue ...