gpt4 book ai didi

java - 在同一模拟对象上模拟不同方法时出现错误

转载 作者:行者123 更新时间:2023-12-02 04:00:12 26 4
gpt4 key购买 nike

这是我的源类。

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)。由于您在 int 上记录 anyLong(),EasyMock 找不到匹配项。

关于java - 在同一模拟对象上模拟不同方法时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56736613/

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