无法模拟返回 Single<?使用 Mockito 扩展 HttpResponse>

unable to mock clientmethod returning Single<? extends HttpResponse> using Mockito

我正在使用 Micronaut 构建微服务。这个特定的服务依赖于另一个服务自动生成的客户端,我想在我的组件测试中模拟它。

但是我无法模拟 apiClient 的其中一种方法。它的签名看起来像这样:

    Single<? extends HttpResponse> patchProfile(String userId, UserprofileApiEntity userProfile);

其中 HttpResponse 来自包 io.micronaut.httpSingle 来自包 io.reactivex

在我的测试中,我试图这样模拟这个端点:

        when(userProfileClientMock.patchProfile(userIdCaptor.capture(), profileCaptor.capture())).thenReturn(Single.just(HttpResponse.ok()));

但是我收到了错误

error: no suitable method found for thenReturn(Single<CAP#1>)
        when(userProfileClientMock.patchProfile(userIdCaptor.capture(), profileCaptor.capture())).thenReturn(response);
                                                                                                 ^
    method OngoingStubbing.thenReturn(Single<CAP#2>) is not applicable
      (argument mismatch; Single<CAP#1> cannot be converted to Single<CAP#2>)
    method OngoingStubbing.thenReturn(Single<CAP#2>,Single<CAP#2>...) is not applicable
      (argument mismatch; Single<CAP#1> cannot be converted to Single<CAP#2>)
  where CAP#1,CAP#2 are fresh type-variables:
    CAP#1 extends HttpResponse from capture of ? extends HttpResponse
    CAP#2 extends HttpResponse from capture of ? extends HttpResponse
Note: /home/anders/Documents/minecraft/minecraft.api.public.displaynames/minecraft.api.public.displaynames.server/src/test/java/net/minecraft/api/pub/displaynames/DisplaynamesApiTest.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

从我收集到的信息来看,它与签名的 <? extends . . .> 部分有关,我已经成功地从类似的 apiclients 中模拟了不同的方法,返回诸如 Single<Foo>Maybe<Bar> 之类的东西 不幸的是,在这种情况下更改客户端的签名没有任何意义。

我试过将单个响应指定为变量,将其显式键入 Single<? extends HttpResponse>Single<MutableHttpResponse> 22=]

任何想法可能是什么原因造成的,以及如何解决它?

所以我最终能够解决这个问题,使用不同的 mockito 语法,即

doReturn(foo).when(bazMock).barMethod();

这种模拟方式不提供类型安全,这是一种权衡,但这里似乎是一个必要的邪恶。我想这适用于任何你想模拟返回通用 class 类型为 foo<? extends bar>

的情况