public void export(CreditBalDistributionQueryDTO creditBalDistributionQueryDTO) { UserInfo uselnfo=UserContext.getUserinfo if (uselnfo == null) { log.info("获取登陆用户信息失败"); throw new BizException("获取登陆用户信息失败”) String instNo=uselnfo.getinstNo0; String instSname=commoninstService.getinstSnameByInstNo(instNo); List<List<String>> headData=new ArrayList<>0; String tpname=ReportCommoninfo.getintqlTpByCode(creditBalDistributionQueryDTO.getintglTpCd0); List<String>column1Head=new ArrayList<>0; String str=制表单位:"+instSname+"报表时间:"+ creditBalDistributionQueryDTO.getStatisDate +"积分类型:"+tpname; column1Head.add(“积分余额分布统计报表(按客户)”) column1Head.add(str); column1Head.add(“积分余额分段"); headData.add(column1Head); String filePath="excel/客户积分余额分布统计表查询报表.xlsx” File file = new File(filePath); if(!file.getParentFile().exists0) file.getParentFile().mkdirs); try(EasyBigExcelHandler handle =new EasyBigExcelHandler<CreditBalDistributionDO CreditBalDistributionDO>(1000,CreditBalDistributionDO.class,file,headData)) handle.setSheetNamePrefix("客户积分余额分布统计表查询报表"); sqlSessionTemplate.select ("com.psbc.pcps.credit.report.operateanalyze.dao.CreditBalDistributionMapper.pageQu ery". creditBalDistributionQueryDTO,handle): sqlSessionTemplate.clearCache0:} catch (IOException e) { //根据业务需要转换成BizException或者进行其他处理 throw new BizException("导出文件失败"); HttpServletResponse response= ((ServletRequestAttributes) RequestContextHolden .getRequestAttributes0).getResponse): try( SecureHttpUtils.exportExcel(response, file, file.getName(). true))catch(IOException e)( log.error("导出报表失败:*+e.getMessage())}请编写单元测试案例,需要使用mockito.lent.when.thenreturn
时间: 2024-03-26 07:40:00 浏览: 96
微信小程序wx.getUserInfo授权获取用户信息(头像、昵称)的实现
Sure, here's an example unit test case using Mockito:
```
@RunWith(MockitoJUnitRunner.class)
public class ExportTest {
@Mock
private UserInfo userInfo;
@Mock
private CommoninstService commoninstService;
@Mock
private ReportCommoninfo reportCommoninfo;
@Mock
private EasyBigExcelHandler<CreditBalDistributionDO> easyBigExcelHandler;
@Mock
private SqlSessionTemplate sqlSessionTemplate;
@Mock
private HttpServletResponse response;
@InjectMocks
private Export export;
@Test
public void testExport() throws Exception {
// Set up test data
CreditBalDistributionQueryDTO creditBalDistributionQueryDTO = new CreditBalDistributionQueryDTO();
creditBalDistributionQueryDTO.setintglTpCd0("123");
creditBalDistributionQueryDTO.setStatisDate("2022-01-01");
when(UserContext.getUserinfo()).thenReturn(userInfo);
when(userInfo.getinstNo0()).thenReturn("456");
when(commoninstService.getinstSnameByInstNo("456")).thenReturn("Test Institution");
when(reportCommoninfo.getintqlTpByCode("123")).thenReturn("Test Type");
List<List<String>> headData = new ArrayList<>();
List<String> column1Head = new ArrayList<>();
column1Head.add("积分余额分布统计报表(按客户)");
column1Head.add("制表单位:Test Institution报表时间:2022-01-01积分类型:Test Type");
column1Head.add("积分余额分段");
headData.add(column1Head);
File file = new File("excel/客户积分余额分布统计表查询报表.xlsx");
// Set up mock behavior for sqlSessionTemplate
List<CreditBalDistributionDO> result = new ArrayList<>();
CreditBalDistributionDO data = new CreditBalDistributionDO();
when(sqlSessionTemplate.select("com.psbc.pcps.credit.report.operateanalyze.dao.CreditBalDistributionMapper.pageQuery", creditBalDistributionQueryDTO, easyBigExcelHandler)).thenReturn(result);
// Call the method under test
export.export(creditBalDistributionQueryDTO);
// Verify expected behavior
verify(UserContext, times(1)).getUserinfo();
verify(userInfo, times(1)).getinstNo0();
verify(commoninstService, times(1)).getinstSnameByInstNo("456");
verify(reportCommoninfo, times(1)).getintqlTpByCode("123");
verify(sqlSessionTemplate, times(1)).select("com.psbc.pcps.credit.report.operateanalyze.dao.CreditBalDistributionMapper.pageQuery", creditBalDistributionQueryDTO, easyBigExcelHandler);
verify(easyBigExcelHandler, times(1)).setSheetNamePrefix("客户积分余额分布统计表查询报表");
verify(sqlSessionTemplate, times(1)).clearCache();
verify(response, times(1)).setContentType("application/octet-stream");
verify(response, times(1)).setHeader(eq("Content-Disposition"), anyString());
verify(SecureHttpUtils, times(1)).exportExcel(response, file, file.getName(), true);
}
}
```
阅读全文