Spring Boot Controller单元测试
一、创建Controller
一个方法是用传统IO来下载文件,一个是NIO下载文件
@Controller
public class FileController { private Logger log = LoggerFactory.getLogger(FileController.class); @RequestMapping(value="/download/oldio}", method = RequestMethod.GET)
public void download(HttpServletRequest request, HttpServletResponse response,String fileName) throws IOException{
String folder = "C://Users/xxx/Downloads/0714";
File file = new File(folder, fileName);
if(file.exists()){
response.setContentType("MimeType");
response.addHeader("Content-Disposition","attachment;filename=" +fileName);
response.setContentLength((int)file.length()); //Java IO
InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
//Copy bytes from source to destination(outputstream in this example), closes both streams.
FileCopyUtils.copy(inputStream, response.getOutputStream()); log.info("download success! ---" + fileName); }else {
throw new Error("file not exist");
}
} @RequestMapping(value="/download/nio}", method = RequestMethod.GET)
public void downloadfornio(HttpServletRequest request, HttpServletResponse response, String fileName) throws IOException{
String folder = "C://Users/xxx/Downloads/0714";
File file = new File(folder, fileName);
if(file.exists()){
response.setContentType("MimeType");
response.addHeader("Content-Disposition","attachment;filename=" +fileName);
response.setContentLength((int)file.length()); //128 * 1024 = 128K
int bufferSize = 131072 * 6;
FileInputStream fileInputStream = new FileInputStream(file);
FileChannel fileChannel = fileInputStream.getChannel();
// 6 * 128K = 768K = 786432
ByteBuffer buffer = ByteBuffer.allocateDirect(786432);
byte[] byteArr = new byte[bufferSize];
int nRead, nGet; try {
while ((nRead = fileChannel.read(buffer)) != -1){
if(nRead == 0){
continue;
}
buffer.position(0);
buffer.limit(nRead);
while (buffer.hasRemaining()){
nGet = Math.min(buffer.remaining(), bufferSize);
// read bytes from disk
buffer.get(byteArr,0,nGet);
//write bytes to output
response.getOutputStream().write(byteArr);
}
buffer.clear(); }
log.info("download success! ---" + fileName);
}catch (IOException e){
e.printStackTrace();
}finally {
buffer.clear();
fileChannel.close();
fileInputStream.close();
} }else {
throw new Error("file not exist");
}
}
}
二、创建单元测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class FileControllerTest { private MockMvc mockMvc; @Autowired
private WebApplicationContext wac; private FileController fc; @Before
public void setup(){
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
fc = this.wac.getBean(FileController.class);
} @Test
public void compareTime() throws Exception { String fileName = "11.tar.gz";
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse(); long c = System.currentTimeMillis();
fc.downloadfornio(request, response, fileName);
long d = System.currentTimeMillis();
System.out.println("nio download takes :" + (d - c) ); long a = System.currentTimeMillis();
fc.download(request, response,fileName);
long b = System.currentTimeMillis();
System.out.println("io download takes :" + (b - a) ); } }
输出结果
nio download takes :144
io download takes :164
Spring Boot Controller单元测试的更多相关文章
- Spring Boot学习——单元测试
本随笔记录使用Spring Boot进行单元测试,主要是Service和API(Controller)进行单元测试. 一.Service单元测试 选择要测试的service类的方法,使用idea自动创 ...
- Spring Boot干货系列:(十二)Spring Boot使用单元测试(转)
前言这次来介绍下Spring Boot中对单元测试的整合使用,本篇会通过以下4点来介绍,基本满足日常需求 Service层单元测试 Controller层单元测试 新断言assertThat使用 单元 ...
- Spring Boot 的单元测试
Spring Boot 的单元测试 引入依赖 testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-tes ...
- Spring Boot使用单元测试
一.Service层单元测试: 代码如下: package com.dudu.service;import com.dudu.domain.LearnResource;import org.junit ...
- 学习 Spring Boot:(二十九)Spring Boot Junit 单元测试
前言 JUnit 是一个回归测试框架,被开发者用于实施对应用程序的单元测试,加快程序编制速度,同时提高编码的质量. JUnit 测试框架具有以下重要特性: 测试工具 测试套件 测试运行器 测试分类 了 ...
- Spring Boot Mock单元测试学习总结
单元测试的方法有很多种,比如使用Postman.SoapUI等工具测试,当然,这里的测试,主要使用的是基于RESTful风格的SpringMVC的测试,我们可以测试完整的Spring MVC流程,即从 ...
- Spring Boot 的单元测试和集成测试
学习如何使用本教程中提供的工具,并在 Spring Boot 环境中编写单元测试和集成测试. 1. 概览 本文中,我们将了解如何编写单元测试并将其集成在 Spring Boot 环境中.你可在网上找到 ...
- Spring Boot 2 单元测试
开发环境:IntelliJ IDEA 2019.2.2Spring Boot版本:2.1.8 IDEA新建一个Spring Boot项目后,pom.xml默认包含了Web应用和单元测试两个依赖包.如下 ...
- Spring MVC Controller 单元测试
简介 Controller层的单元测试可以使得应用的可靠性得到提升,虽然这使得开发的时间有所增加,有得必失,这里我认为得到的比失去的多很多. Sping MVC3.2版本之后的单元测试方法有所变化,随 ...
随机推荐
- js学习之数据结构和算法
js中的数据结构 1.列表 待办事项列表.购物清单.最佳十名榜单等等. 适用: 1)数据结构较为简单, 2)不需要在一个长序列中查找元素,或者对其进行排序 2.栈 一摞盘子 ----- 添加删除只能从 ...
- 前端获取文件input框的美化操作
前面我们说了一种利用input框和js的当时获取本地文件内容的情况-详细信息参考 2017年11月8日前端用js获取本地文件的内容 以上方式获取的按钮是系统默认的显示,有时候我们需要对按钮的外观进行美 ...
- ML-对偶(Duality)问题 KKT 条件
Primal => Dual 现实中我们遇到的原优化问题, 写为标准型的话是这样的. \(min _w f(w) \\ s.t. \\ g_i(w) <=0 \\ h_i(w) = 0\) ...
- Django 之 CBV
Django 中有两种编写方式,FBV 和 CBV,那么什么是 FBV,CBV 又是什么呢? 一.什么是 CBV FBV(function base views) 就是在视图里使用函数处理请求(常见) ...
- zabbix--基本操作
zabbix 快速上手 示例一些zabbix的最基本的配置: 添加主机群组:添加主机:创建监控项:创建触发器 添加主机群组 参考官档:https://www.zabbix.com/documentat ...
- php的选择排序
往前. <?php /** * 选择排序 * 工作原理是每次从待排序的元素中的第一个元素设置为最小值, * 遍历每一个没有排序过的元素,如果元素小于现在的最小值, * 就将这个元素设置成为最小值 ...
- Vue基本用法
在学习Vue的基本用法之前,我们先简单的了解一些es6的语法 let: 特点:1.局部作用域 2.不会存在变量提升 3.变量不能重复声明 const: 特点:1.局部作用域 2.不会存在变量提升 3. ...
- 【P1889】SOLDIERS (中位数)
题目描述 在一个划分成网格的操场上, n个士兵散乱地站在网格点上.由整数 坐标 (x,y) 表示.士兵们可以沿网格边上.下左右移动一步,但在同时刻任一网格点上只能有名士兵.按照军官的命令,们要整齐地列 ...
- SpringBoot项目的测试类
1. package soundsystem; import static org.junit.Assert.*; import org.junit.Test; import org.junit.ru ...
- flutter 项目中,开发环境、多接口域名、多分支的配置
flutter 项目中,开发环境.多接口域名.多分支的配置 开发环境:配置成多个入口文件.比如:main.dart.main_develop.dart.main_preview.dart 多域名:每个 ...