我想知道如何通过rxjava2测试Retrofit2调用。我的改装API接口的样子:如何测试Retrofit2 + RxJava2 api调用?
public interface LoginApiMapping {
@POST("v1/secm/oam/oauth2/token")
Observable<Response<RestResponseHolder<LoginResponseModel>>> login(@Body LoginModel model);
}
,我想编写测试,这将通过RxJava2发送该请求,并检查响应。我认为有与RxJava的问题,因为它是异步的,在测试完成之前,我得到的回应,所以我试图如下使用TestSubscriber,但是这是不可能的认购TestSubscriber <响应< RestResponseHolder < LoginResponseModel > > >我预计
@Test
public void loginTest(){
Context appContext = InstrumentationRegistry.getTargetContext();
Api t = TestApi.create(appContext);
LoginModel loginModel = new LoginModel("username","password");
TestSubscriber<Response<RestResponseHolder<LoginResponseModel>>> testSubscriber = new TestSubscriber<>();
t.get(LoginApiMapping.class).login(loginModel).subscribe(testSubscriber);
}
谁解决了这个问题?感谢
非常感谢您的回复,我认为它会很有用,但首先我需要能够订阅TestSubscriber>>什么是不可能的。也许它可能有一些通用性的问题?在这个例子中,它似乎与对象RubberChicken一起工作http://fedepaol.github.io/blog/2015/09/13/testing-rxjava-observables-subscriptions/。也许它的RxJava2?当我尝试订阅我的对象时,我甚至无法编译它。 –
Houskov