0

Here is my source class.

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();
        }
}

Here is my test class:

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);


}

Tried with mocking only one method also but same issue.In my source file already EasyMock is used so i continued with that only. I'm unable to find where i did mistake.Getting the error repositoryService.getTotalRecords(repId, queryParamMap, "like"); at this point in source code :

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
mae
  • 117
  • 2
  • 12
  • Possible duplicate of [EasyMock "Unexpected method call" despite of expect method declaration](https://stackoverflow.com/questions/31966388/easymock-unexpected-method-call-despite-of-expect-method-declaration) – Dherik Jun 24 '19 at 13:42
  • @Dherik , mentioned link addressed 1.returning different values for the same method 2.They even did not use anyObject function for mocking.But in my case calling two different functions and i used mock to pass arguments. – mae Jun 25 '19 at 06:14

1 Answers1

0

I suspect that the expectation is wrong. You used

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

But it feels like the definition of getTotalRecords is long getTotalRecords(int, Map, String). Since you are recording anyLong() on a int, EasyMock doesn't find a match.

Henri
  • 5,551
  • 1
  • 22
  • 29