我正在尝试使用Powermock
模拟一个arraylist PowerMockito.mockStatic(DailyReceiptsAndExceptionsDetailsDao.class);
PowerMockito.mockStatic(UtilityFunctions.class);
DailyReceiptsAndExceptionsExport dailyExceptionsExport = Mockito.mock(DailyReceiptsAndExceptionsExport.class);
List<DailyReceiptsAndExceptionsDetailsGridDto> resultList = getDailyExceptions(inputDto);
try{
PowerMockito.whenNew(DailyReceiptsAndExceptionsExport.class).withArguments(Mockito.any(DailyReceiptsAndExceptionsDetailsInputDto.class)).thenReturn(dailyExceptionsExport);
Mockito.when(DailyReceiptsAndExceptionsDetailsDao.getDailyReceiptsAndExceptions(Mockito.any(DailyReceiptsAndExceptionsDetailsInputDto.class))).thenReturn(resultList);
Mockito.when(UtilityFunctions.processReportSchedule(scheduleId, jobId,dailyExceptionsExport,(List<DailyReceiptsAndExceptionResultDTO>)Mockito.any(), null, null)).thenReturn(true);
}catch(Exception e){
}
我需要为下面的课程编写测试。
public static Response getOutboundAvgCubeAndWeightUtilization(
@QueryParam("dc") String dc,
@QueryParam("asn") String asn,
@QueryParam("sortBy") String sort,
@QueryParam("isExport") boolean isExport,
@QueryParam("fileType") String fileType,
@QueryParam("scheduleId") BigDecimal scheduleId,
@QueryParam("jobId") BigDecimal jobId) {
ResponseDTO responseDto = new ResponseDTO();
DailyReceiptsAndExceptionsDetailsInputDto inputDto = new DailyReceiptsAndExceptionsDetailsInputDto ();
inputDto.setAsn(asn);
inputDto.setDc(dc);
inputDto.setSortBy(sort);
inputDto.setFileType(fileType);
inputDto.setExport(isExport);
String filePath = "";
try {
DailyReceiptsAndExceptionResultDTO resultDto = DailyReceiptsAndExceptionsDetailsBusinessManager.getInstance().manageDailyReceiptsAndExceptionsDetails(inputDto);
List<DailyReceiptsAndExceptionResultDTO> resultsList = new ArrayList<DailyReceiptsAndExceptionResultDTO>();
resultsList.add(resultDto);
if(scheduleId != null) {
boolean responseStatus = UtilityFunctions.processReportSchedule(scheduleId, jobId, new DailyReceiptsAndExceptionsExport(inputDto), resultsList, null,null);
responseDto.setResult(Boolean.toString(responseStatus));
return CommonUtil.convertResponseToJson(responseDto);
}
}
我的测试课程如下。
@Test
public void testGetOutboundAvgCubeAndWeightUtilization_4()
throws Exception {
String dc = "5854";
String asn = "*";
String sort = "SKU";
boolean isExport = false;
String fileType = "";
BigDecimal scheduleId = new BigDecimal(100);
BigDecimal jobId = new BigDecimal(100);
DailyReceiptsAndExceptionsDetailsInputDto inputDto = new DailyReceiptsAndExceptionsDetailsInputDto ();
inputDto.setAsn(asn);
inputDto.setDc(dc);
inputDto.setSortBy(sort);
inputDto.setFileType(fileType);
inputDto.setExport(isExport);
DailyReceiptsAndExceptionsDetailsMockDAO.mockgetDailyExceptions(inputDto, scheduleId, jobId);
Response result = DailyReceiptsAndExceptionsDetailsService.getOutboundAvgCubeAndWeightUtilization(dc, asn, sort, isExport, fileType, scheduleId, jobId);
String output = result.getEntity().toString();
assertEquals(true,output.contains("\"result\": \"true\""));
}
当iam运行测试用例时,它会抛出错误,因为我觉得列表的模拟不正确。
任何人都可以告诉你如何运行这个测试场景......
答案 0 :(得分:3)
你的嘲讽似乎很好。
JUnit正在测试失败,因为该行
assertEquals(true,output.contains("\"result\": \"true\""));
失败:这意味着您的字符串output
不包含文字"result": "true"
也许找出错误的一种方法是在assertEquals()
调用之前打印出输出值,或者使用调试器来查看输出值是什么。
作为旁注,assertEquals(true, <condition>)
非常详细,您可以使用assertTrue(<condition>)
代替。
答案 1 :(得分:2)
根据你的评论,测试只是失败了。 (AssertionErrors是JUnit说你的测试失败的方式。)
如果您使用Hamcrest,则可能会收到更好的错误消息。因此,您必须更改代码的最后两行:
assertThat(result.getEntity(), hasToString(containsString("\"result\": \"true\"")));
为org.hamcrest.MatcherAssert.assertThat
和org.hamcrest.Matchers.*
添加一些静态导入。
新的错误消息可能会帮助您找到错误。