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版本之后的单元测试方法有所变化,随 ...
随机推荐
- 网络编程系统化学习(1.1.)--socket基础
大纲 学完该阶段内容,你将会很好的完成如下的面试题 socket面试试题相关: 1.写一个简单的socket程序,实现客户端发送数据,服务端收到数据后,将该内容转发给客户端 2.简要概述一下socke ...
- css实现弹框垂直居中
原文链接:https://blog.csdn.net/sunny327/article/details/47419949/ <!DOCTYPE html><html> < ...
- 我的windows开发环境设定与日常使用指南
目录 开发相关的软件包安装.设定 Visual Studio 默认设定 鼠标右键添加"在此处打开cmd"选项 git gvim notepad++ VSCode-Insider C ...
- 浅谈原子操作、volatile、CPU执行顺序
浅谈原子操作.volatile.CPU执行顺序 在计算机发展的鸿蒙年代,程序都是顺序执行,编译器也只是简单地翻译指令,随着硬件和软件的飞速增长,原来的工具和硬件渐渐地力不从心,也逐渐涌现出各路大神在原 ...
- jenkins发布PHP代码(三)
一.先检查是否安装Git plugin和Publish Over SSH插件 系统管理-->插件管理-->已安装插件-->搜索Git plugin和Publish Over SSH ...
- UGUI和NGUI的优化分享
学习资料 来自UWA的分享,针对于Unity 4.x 及5.3 以下版本,Unity5.5及更高版本可能适用. 文章:UWA技术直播视频集锦 UGUI &NGUI http://blog.uw ...
- 开机没有deepin启动项的解决办法
增加efi/deepin/grubx64.efi的启动项 问题描述 打开电脑,进入bios没有deepin启动项 解决办法 进入bios setup,选择boot sourquense,选择uefi, ...
- 纯数据结构Java实现(7/11)(SegmentTree)
欢迎访问我的自建博客: CH-YK Blog.
- Broadcast,Scatter,Gather,Reduce,All-reduce分别是什么?
Broadcast 看名字就很好理解了,其实就是把同一份数据分发广播给所有人,示意图如下: Scatter 不同于Broadcast, scatter可以将不同数据分发给不同的进程. Gather 这 ...
- js--动画
运动框架实现思想1.速度(改变值left,right,width,height,opacity)2.缓冲运动3.多物体运动4.任意值变化5.链式运动6.同时运动 我们先来介绍第一章改变left值来使物 ...