gpt4 book ai didi

junit - 使用 PwerMock 模拟 ArrayList

转载 作者:行者123 更新时间:2023-12-02 21:49:32 25 4
gpt4 key购买 nike

我正在尝试使用 Powermock 模拟数组列表,如下所示

MockDao 类

 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){
}

我需要为以下类(class)编写测试。

    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\""));
}

当我运行测试用例时,它抛出错误,因为我认为列表的模拟不正确。

任何人都可以告诉如何运行这个测试场景....

最佳答案

你的模拟看起来没问题。

JUnit 未通过测试,因为该行

assertEquals(true,output.contains("\"result\": \"true\""));

失败:这意味着您的字符串 output不包含文本 "result": "true"

也许您找出问题所在的一种方法是打印 assertEquals() 之前的输出值。调用或使用调试器来查看输出的值是什么。

作为旁注,assertEquals(true, <condition>)非常冗长,你可以使用assertTrue(<condition>)相反。

关于junit - 使用 PwerMock 模拟 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18910688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com