public void exportToExcel(CreditBatchAdjExchStatementQueryDTO dto,HttpServletResponse response) { log.info("批调类积分兑换情况统计报表导出开始,参数--》}",dto); File file =creditBatchAdjExchStatementService.export(dto); String fileName=""; try { fileName=URLEncoder.encode(file.getName0,"UTF-8").replaceAll("\\+", "%20");}catch(UnsupportedEncodingExceptione) throw new BizException("文件导出错误") try { SecureHttpUtils.exportExcel(response,file,file.getName,true); }catch(IOExceptione){ throw new BizException("文件导出错误"); log.info("批调类积分兑换情况统计报表导出,结束"); 请根据以上代码编写单元测试,mock掉service层的内容,需要中文注释
时间: 2023-07-19 22:56:17 浏览: 84
vue el-table前端 file-saver 导出excel可导出分页数据.zip
// 导出Excel功能单元测试
@Test
public void testExportToExcel() throws Exception {
// 构造参数
CreditBatchAdjExchStatementQueryDTO dto = new CreditBatchAdjExchStatementQueryDTO();
dto.setStartDate("2022-01-01");
dto.setEndDate("2022-01-31");
dto.setPageNum(1);
dto.setPageSize(10);
// mock service层的返回值
File file = new File("test.xlsx");
CreditBatchAdjExchStatementService service = Mockito.mock(CreditBatchAdjExchStatementService.class);
Mockito.when(service.export(dto)).thenReturn(file);
// 构造response对象
HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
OutputStream outputStream = Mockito.mock(OutputStream.class);
Mockito.when(response.getOutputStream()).thenReturn(outputStream);
// 执行导出Excel功能
CreditBatchAdjExchStatementController controller = new CreditBatchAdjExchStatementController();
controller.setCreditBatchAdjExchStatementService(service);
controller.exportToExcel(dto, response);
// 验证导出的文件名是否正确
String expectedFileName = URLEncoder.encode(file.getName(), "UTF-8").replaceAll("\\+", "%20");
Mockito.verify(response).setHeader("Content-Disposition", "attachment;filename=" + expectedFileName);
// 验证导出的文件内容是否正确
Mockito.verify(service).export(dto);
Mockito.verify(outputStream).write(Mockito.any(byte[].class));
Mockito.verify(outputStream).flush();
Mockito.verify(outputStream).close();
}
阅读全文