如何在RxJava服务器轮询中单元测试“repeatWhen”

时间:2017-04-21 12:55:21

标签: java android unit-testing mockito rx-java

这是我的代码

return Observable.defer(() -> mApi.verifyReceipt(username, password))
            .subscribeOn(Schedulers.io())
            .doOnNext(this::errorCheck)
            .repeatWhen(observable -> observable.delay(1, TimeUnit.SECONDS))
            .takeUntil(Response::body)
            .filter(Response::body)
            .map(Response::body);

它保持轮询并收到“false”布尔响应,并在收到“true”时停止轮询。我为onNext()创建了一个测试用例,例如:

@Test
public void testVerifyReceiptSuccessTrueResponse() {
    Response<Boolean> successTrueResponse = Response.success(Boolean.TRUE);
    when(mMockApi.verifyReceipt(any(), any())).thenReturn(Observable.just(successTrueResponse));

    mService.verifyReceipt("", "").subscribe(mMockVerifyReceiptSubscriber);

    verify(mMockVerifyReceiptSubscriber).onNext(eq(Boolean.TRUE));
}

如果回复是“假”,那么当它继续进行轮询时如何测试情况的​​任何建议?

1 个答案:

答案 0 :(得分:1)

when(mMockApi.verifyReceipt(any(), any()))
            .thenReturn(Observable.just(successFalseResponse))
            .thenReturn(Observable.just(successTrueResponse));

Observable observable = mService.verifyReceipt("", "");

observable.subscribe(mMockVerifyReceiptSubscriber);

// Two approaches here

// First one - sleep as much as specified in delay(), in your case 1 second 
// and then perform verification specified on next line

// Second approach (better) - make your architecture in a way, that you can 
// provide an implementation to `repeatWhen()` operator, in other words, 
// inject. And then you'll be able to inject `observable.delay(1, TimeUnit.SECONDS)`
// for production code and inject retry logic without delay for test code.

// Now we are verifying that because of first time `false` was returned,
// then retryWhen() logics are performed
verify(observable).delay(1, TimeUnit.SECONDS); // here should be the injected implementation

verify(mMockVerifyReceiptSubscriber).onNext(eq(Boolean.TRUE));

现在,第一次观察会发出false,这会调用retryWhen(),下次true会被发出。