我有下面的類(簡體):如何測試observable在RxJava中使用正確的調度程序?
public class Usecase<T> {
private final Observable<T> get;
private final Scheduler observeScheduler;
public Usecase(Observable<T> get, Scheduler observeScheduler) {
this.get = get;
this.observeScheduler = observeScheduler;
}
public Observable<T> execute() {
return get.subscribeOn(Schedulers.io()).observeOn(observeScheduler);
}
}
而且我編寫單元測試它。我如何測試subscribeOn
和observeOn
是否被調用了正確的值?
我嘗試以下方法:
Observable<String> observable = mock(Observable.class);
Usecase<String> usecase = new Usecase(observable, Schedulers.computation());
usecase.execute();
verify(observable).subscribeOn(Schedulers.computation()); // should fail here, but passes
verify(observable).observeOn(Schedulers.computation()); // should pass, but fails: Missing method call for verify(mock) here
上述失敗(我認爲),因爲subscribeOn
和observeOn
是final
方法。那麼可能有其他方法可以確保observable使用正確的調度程序?