【发布时间】:2019-02-11 15:33:09
【问题描述】:
我正在使用 Micronaut 构建一个微服务。这个特定的服务依赖于从另一个服务自动生成的客户端,我想在我的组件测试中模拟它。
但是我无法模拟 apiClient 的方法之一。它的签名是这样的:
Single<? extends HttpResponse> patchProfile(String userId, UserprofileApiEntity userProfile);
其中HttpResponse 来自包io.micronaut.http,Single 来自包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> 认为可能 mockito 以某种方式被类型擦除弄糊涂了,但无济于事。
任何想法可能是什么原因,以及如何解决它?
【问题讨论】:
标签: java testing mockito rx-java2 micronaut