简体   繁体   English

Mockito 验证失败,通缉但未调用,但它确实有效

[英]Mockito verify fails with Wanted but not invoked but it actually works

I'm trying to do a refactor of some ifs that sonar os crying about.我正在尝试对声纳操作系统哭泣的一些 ifs 进行重构。 Before the refactor, the code looks like this:在重构之前,代码如下所示:

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

So I added the methods to a map and call it like this所以我将这些方法添加到 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);
}

I didn't modify anything in the test class, but I'm getting this error:我没有在测试 class 中修改任何内容,但出现此错误:

Wanted but not invoked:通缉但未调用:

rtvEnhancer.getLookupCodes(
    <any>,
    <any>,
    [CodeTypeRequest.EventCode(codeType=FromDisposition, codeValue=ATS), CodeTypeRequest.EventCode(codeType=ToDisposition, codeValue=DIST), CodeTypeRequest.EventCode(codeType=ReasonCode, codeValue=DRT)],
    RTV
);

-> at com.inventory.alfred.enhancement.AbstractEnhancerMapper.getLookupCodes(AbstractEnhancerMapper.java:172) -> 在 com.inventory.alfred.enhancement.AbstractEnhancerMapper.getLookupCodes(AbstractEnhancerMapper.java:172)

But debbuging it, it actually works and the assertion is correct.但是调试它,它确实有效并且断言是正确的。 What could be the reason?可能是什么原因?

If you are sure that this code如果您确定此代码

rtvEnhancer.getLookupCodes(
    <any>,
    <any>,
    [CodeTypeRequest.EventCode(codeType=FromDisposition, codeValue=ATS), CodeTypeRequest.EventCode(codeType=ToDisposition, codeValue=DIST), CodeTypeRequest.EventCode(codeType=ReasonCode, codeValue=DRT)],
    RTV);

is called then probably one of the argument does not overrides equals() and mockito cant match actual arguments with stubbed ones.被调用然后可能参数之一不会覆盖equals()和 mockito 不能匹配实际的 arguments 与存根的。 Without equals they are matched by Object.equals() which is memory address.没有等号,它们与 Object.equals() 匹配,这是 memory 地址。 My guess is CodeTypeRequest.EventCode我的猜测是CodeTypeRequest.EventCode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM