Created
March 16, 2018 10:32
-
-
Save ValeriusGC/c6557eb82fdcfb7f66f1c8ef07f56624 to your computer and use it in GitHub Desktop.
RxJava2. How make Observable from VoidMethod. And how test Rx-Exceptions with JUnit.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.reactivex.Completable; | |
import io.reactivex.disposables.Disposable; | |
import io.reactivex.functions.Action; | |
import io.reactivex.observers.DisposableCompletableObserver; | |
import io.reactivex.observers.TestObserver; | |
import org.junit.Test; | |
import java.util.Observable; | |
public class RxFromAction { | |
/** | |
* Just function. | |
* @throws Exception | |
*/ | |
void f() throws Exception { | |
throw new Exception("was false"); | |
} | |
/** | |
* {@link Observable} from function. | |
* @return | |
*/ | |
Completable of () { | |
return Completable.fromAction(this::f); | |
} | |
/** | |
* Check for 'onError' has been catched. | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
Disposable d = new RxFromAction().of().subscribeWith(new DisposableCompletableObserver() { | |
@Override | |
public void onComplete() { | |
System.out.println("onComplete"); | |
} | |
@Override | |
public void onError(Throwable throwable) { | |
System.out.println("onError: " + throwable.getLocalizedMessage()); | |
} | |
}); | |
d.dispose(); | |
} | |
/** | |
* How to test for error! | |
*/ | |
@Test | |
public void test() { | |
TestObserver to = new TestObserver(); | |
of().subscribe(to); | |
to.assertError(Exception.class); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment