Spring Boot doCallRealMethod对void返回类型方法不起作用

ljsrvy3e  于 2022-11-23  发布在  Spring
关注(0)|答案(1)|浏览(157)

我正在尝试调用一个返回类型为void的真实的方法。我已经在我的bean上尝试了@mock和@spy,但是它仍然没有进入实际的类。

@Autowired private HelperV2 authValidationHelper;

@Override
public void postProcess(Event event) {
List<Error> errorList = new ArrayList<>();
  authValidationHelper.validateOrderTotalAmount(event, errorList);
  handleError(event, errorList);
}
}

测试用例

@Spy private HelperV2 authValidationHelper;

@Test
public void test_postProcess()  {
BaseOrderEvent event =
    ParserUtil.getObjectFromFile(
        "events/createPaymentRequestWithShoppingCardOnly2.json", Event.class);
Mockito.doCallRealMethod()
    .when(authValidationHelper)
    .validateOrderTotalAmount(Mockito.any(), Mockito.any());
createOrderEventProcessor.postProcess(event);
}
6g8kf2rb

6g8kf2rb1#

如果要替换Spring上下文中的Bean,则需要@MockBean@SpyBean

相关问题