这是我的源代码类。

public class Main{
    public static Response getImagesForImageRepository(long[] repId,
                RepositoryService repositoryService,
                Map<String, String> queryParamMap, String range,
                Map<String, String> sortConditions) {
            if (repId == null)
            {
                long[] repoId = {SwimConstants.SWIM_IMAGE_REPOSITORY_ID,SwimConstants.SWIM_FILESYSTEM_REPOSITORY_ID};
                repId = repoId;
            }
            long totalSize;
                totalSize = repositoryService.getTotalRecords(repId, queryParamMap,
                        "like");

                if (totalSize < end) {
                    end = (int) totalSize;
                }
                String imageType = originalQueryMap.get("imageType");
                if (imageType == null) {
                    swImageInfoList = repositoryService.searchImage(repId,
                            originalQueryMap, "like", start,
                            rangeObj.getStartRow() == 0
                                    && rangeObj.getEndRow() == 0 ? 0 : end - start
                                    + 1, sortConditions);
                } else {
                    swImageInfoList = repositoryService.searchImage(repId,
                            originalQueryMap, "like", start,
                            rangeObj.getStartRow() == 0
                                    && rangeObj.getEndRow() == 0 ? 0 : end - start
                                    + 1, sortConditions);
                }

            }
            return responseBuilder.build();
        }
}


这是我的测试课:

public void testgetImagesForImageRepository() {
        RepositoryService repositoryServiceMock=EasyMock.createMock(RepositoryService .class);
        long[] repId = null;
        Map<String, String> queryParamMap = new HashMap<>();
        queryParamMap.put("imageType", "imageType");
        String range = "range=123-4";
        Map<String, String> sortConditions = new HashMap<>();
        List<SoftwareImageInfo> swImageInfoList = new ArrayList<>();
        SoftwareImageInfo imageInfo = new SoftwareImageInfo();
        imageInfo.setName("imageName");
        imageInfo.setFamily("family");
        imageInfo.setDescription("description");
        imageInfo.setUpdatedTime(new Date(2019, 12, 12));
        swImageInfoList.add(imageInfo);
        try {
            EasyMock.expect(repositoryServiceMock.getTotalRecords(EasyMock.anyLong(), EasyMock.isA(Map.class),
                    EasyMock.isA(String.class)))
                    .andReturn(Long.valueOf(2));

            EasyMock.expect(repositoryServiceMock.searchImage(EasyMock.anyLong(), EasyMock.isA(Map.class),
                    EasyMock.isA(String.class), EasyMock.anyInt(), EasyMock.anyInt(),
                    EasyMock.isA(Map.class))).andReturn(swImageInfoList);
        } catch (IfmSwimRepositoryException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        EasyMock.replay(repositoryServiceMock);
        SwimRestHelper.getImagesForImageRepository(repId, repositoryServiceMock,
     queryParamMap, range, sortConditions);


}


尝试仅模拟一种方法,但也遇到了同样的问题。在我的源文件中已经使用EasyMock,因此我仅继续进行。
我找不到我做错了的地方。获取错误repositoryService.getTotalRecords(repId,queryParamMap,
“喜欢”);此时在源代码中:

java.lang.AssertionError:
  Unexpected method call RepositoryService.getTotalRecords([101, 103], {imageType=imageType}, "like"):
    RepositoryService.getTotalRecords(<any>, isA(java.util.Map), isA(java.lang.String)): expected: 1, actual: 0
    RepositoryService.searchImage(<any>, isA(java.util.Map), isA(java.lang.String), <any>, <any>, isA(java.util.Map)): expected: 1, actual: 0

最佳答案

我怀疑期望是错误的。你用过

expect(repositoryServiceMock.getTotalRecords(anyLong(), isA(Map.class), isA(String.class))).andReturn(2L);


但是感觉getTotalRecords的定义是long getTotalRecords(int, Map, String)。由于您正在anyLong()上录制int,因此EasyMock找不到匹配项。

07-26 09:41