2

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

模拟道课

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

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

谁能告诉如何运行这个测试场景....

4

2 回答 2

3

你的模拟似乎很好。

JUnit 未通过测试,因为该行

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

失败:这意味着您的 Stringoutput不包含文本"result": "true"

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

作为旁注,assertEquals(true, <condition>)非常冗长,您可以assertTrue(<condition>)改用。

于 2013-09-26T19:13:04.133 回答
2

根据您的评论,测试只是失败了。(AssertionErrors 是 JUnit 表示您的测试失败的方式。)

如果您使用Hamcrest ,您可能会收到更好的错误消息。因此,您必须更改代码的最后两行:

assertThat(result.getEntity(), hasToString(containsString("\"result\": \"true\"")));

org.hamcrest.MatcherAssert.assertThat为和添加一些静态导入org.hamcrest.Matchers.*

新的错误消息可能会帮助您找到错误。

于 2013-09-25T19:35:59.657 回答