【发布时间】:2021-08-03 12:23:56
【问题描述】:
我有以下单元测试,其中有以下模拟和间谍。
@Spy
private Map<String, String> rulesObjects = new HashMap<>();
@Spy
private Map<String, Map<String, List<String>>> mandatoryFieldObject = new HashMap<>();
@Mock
private configProperties configProperties;
@InjectMocks
private LoaderService loaderService;
@Test
@DisplayName("This test should load the config files in memory")
void initRuleMaps() throws IOException {
this.loaderService.initRuleMaps();
assertThat(this.rulesObjects)
.isNotEmpty()
.hasSize(3);
}
initRuleMaps的逻辑调用这2个方法;
private void saveRulesInCache() throws IOException {
rulesObjects.put(EmvcoConstants.LEGACY, dataLegacy);
rulesObjects.put(EmvcoConstants.LEGACY1, dataLegacy1);
rulesObjects.put(EmvcoConstants.LEGACY2, dataLegacy2);
}
private void saveMandatoryFieldListInCache() throws IOException {
for (Map.Entry<String, String> type: this.configProperties.getTypes().entrySet()) {
Map<String, List<String>> mandatoryRuleFiledMap = getMandatoryRuleFiled(qrType.getKey());
mandatoryFieldObject.put(type.getValue(), mandatoryRuleFiledMap);
}
}
在 initRuleMaps 方法中,两个地图上都有一些计算。每个应有 3 个条目。但是正在发生的事情似乎只创建了一个间谍,并且所有计算都只在一个对象上完成。因此,我得到了 6 个条目。
查看每个间谍的参考资料时,它们实际上是相同的。
有没有办法解决这个问题?
谢谢。
【问题讨论】:
-
不看
initRuleMaps的代码就很难判断。 -
@talex 我更新了这个问题。希望对您有所帮助。