Unit test – how to simulate the improved service observable that calls onerror?
•
Java
I'm testing this code
service.getProducts()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Result<Catalog<SoajsProductPreview>>>() {
@Override
public void onError(Throwable e) {
view.showErrorView(e);
}
@Override
public void onNext(Result<Product> products) {
view.showProducts(products)
}
@Override
public void onCompleted() {}
});
Test view Showproducts() simulates whether the result returned by the service is normal@ H_ 419_ 14 @ I do
when(service.getProducts().thenReturn(someObservable);
Now I want to test the view Showerrorview() is called when the service throws an error, but I can't find a way:
Obviously, the following is not compiled
when(service.getProducts().thenReturn(someException);
This throws an exception immediately, but does not call the subscriber's onerror method
when(service.getProducts().thenReturn(someException);
How to call subscriber onError()?
Solution
when(service.getProducts().thenReturn(Observable.error(someException))
when(service.getProducts().thenReturn(Observable.error(someException))
It should work See the documentation starting here
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
