![](/img/trans.png)
[英]Mockito verify unit test - Wanted but not invoked. Actually, there were zero interactions with this mock
[英]Mockito verify fails with Wanted but not invoked but it actually works
我正在嘗試對聲納操作系統哭泣的一些 ifs 進行重構。 在重構之前,代碼如下所示:
if (isRTVCreatedEventType(eventType)) {
schemaName = RTV_CREATE_SYSTEM_SCHEMA_MAP.get(alfredEventHeader.getSource());
if (MerchandiseReturnToVendorCreateConfirmed.getClassSchema().getName().equals(schemaName)) {
return buildMerchandiseReturnToVendorCreate(alfredInventoryEvent);
}
if (StoreReturnToVendorCreated.getClassSchema().getName().equals(schemaName)) {
return buildStoreReturnToVendorCreate(alfredInventoryEvent);
}
}
所以我將這些方法添加到 map 並像這樣調用它
private final Map<String, Function<AlfredInventoryEvent, SpecificRecordBase>> rtvEventBuilders = Map.ofEntries(
Map.entry(MerchandiseReturnToVendorCreateConfirmed.getClassSchema().getName(), this::buildMerchandiseReturnToVendorCreate),
Map.entry(MerchandiseReturnToVendorCancelConfirmed.getClassSchema().getName(), this::buildMerchandiseReturnToVendorCancel),
Map.entry(MerchandiseReturnToVendorShipmentConfirmed.getClassSchema().getName(), this::buildMerchandiseReturnToVendorShipment),
Map.entry(MerchandiseReturnToVendorUpdateConfirmed.getClassSchema().getName(), this::buildMerchandiseReturnToVendorUpdate),
Map.entry(StoreReturnToVendorCreated.getClassSchema().getName(), this::buildStoreReturnToVendorCreate),
Map.entry(StoreReturnToVendorCanceled.getClassSchema().getName(), this::buildStoreReturnToVendorCancel),
Map.entry(StoreReturnToVendorShipped.getClassSchema().getName(), this::buildStoreReturnToVendorShipment),
Map.entry(StoreReturnToVendorUpdated.getClassSchema().getName(), this::buildStoreReturnToVendorUpdate)
);
if (isRTVCreatedEventType(eventType)) {
schemaName = RTV_CREATE_SYSTEM_SCHEMA_MAP.get(alfredEventHeader.getSource());
return rtvEventBuilders.get(schemaName).apply(alfredInventoryEvent);
}
我沒有在測試 class 中修改任何內容,但出現此錯誤:
通緝但未調用:
rtvEnhancer.getLookupCodes(
<any>,
<any>,
[CodeTypeRequest.EventCode(codeType=FromDisposition, codeValue=ATS), CodeTypeRequest.EventCode(codeType=ToDisposition, codeValue=DIST), CodeTypeRequest.EventCode(codeType=ReasonCode, codeValue=DRT)],
RTV
);
-> 在 com.inventory.alfred.enhancement.AbstractEnhancerMapper.getLookupCodes(AbstractEnhancerMapper.java:172)
但是調試它,它確實有效並且斷言是正確的。 可能是什么原因?
如果您確定此代碼
rtvEnhancer.getLookupCodes(
<any>,
<any>,
[CodeTypeRequest.EventCode(codeType=FromDisposition, codeValue=ATS), CodeTypeRequest.EventCode(codeType=ToDisposition, codeValue=DIST), CodeTypeRequest.EventCode(codeType=ReasonCode, codeValue=DRT)],
RTV);
被調用然后可能參數之一不會覆蓋equals()
和 mockito 不能匹配實際的 arguments 與存根的。 沒有等號,它們與 Object.equals() 匹配,這是 memory 地址。 我的猜測是CodeTypeRequest.EventCode
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.