I have a Single.fromCallable()
that I'm subscribed to. Using LiveData I'm trying to test UI error handling inside .subscribe()
's onError()
callback method.
I've tried throwing Exceptions
, calling disposable.dispose()
, but I can't seem to fake an error so it directly goes to the onError()
block.
Single.fromCallable(() -> remoteRepository.doSomething())
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new SingleObserver<BigInteger>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
walletStatusLiveData.setValue("Now subscribed");
//How do I fake the error here so it jumps to onError() so I can test my error handling UI
throw new IllegalArgumentException(); //Crashes app
throw new RunTimeException(); //Crashes app
try {
throw new IllegalArgumentException(); //This trycatch doesn't do anything and onSuccess is called
}catch (Exception e){
}
d.dispose(); //This completely exits all 3 callbacks.
}
@Override
public void onSuccess(@NonNull BigInteger result) {
walletStatusLiveData.setValue("On success!");
}
@Override
public void onError(@NonNull Throwable e) {
Log.e(TAG, "onError: getWalletBalance Called", e);
walletStatusLiveData.setValue("Testing error UI");
//How do I trigger this callback on onSubscribe or onSuccess?
}
});
What do I call to jump to .onError()
?