Created
December 4, 2015 06:31
-
-
Save keima/de360841402fbf9c82df to your computer and use it in GitHub Desktop.
RxJavaでの非同期テストの書き方練習
This file contains hidden or 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
| package jp.seesaa.example.util; | |
| import org.junit.Test; | |
| import java.util.Arrays; | |
| import java.util.concurrent.TimeUnit; | |
| import rx.Observable; | |
| import rx.functions.Action0; | |
| import rx.functions.Action1; | |
| import rx.observers.TestSubscriber; | |
| import rx.schedulers.Schedulers; | |
| public class RxJavaSandboxTest { | |
| private static final String TAG = RxJavaSandboxTest.class.getSimpleName(); | |
| @Test | |
| public void testObservableConcat() throws Exception { | |
| TestSubscriber<String> testSubscriber = new TestSubscriber<>(); | |
| System.out.println("Start."); | |
| Observable.concat( | |
| asyncObservable("uno", 4), | |
| asyncObservable("dos", 3), | |
| asyncObservable("tres", 2), | |
| asyncObservable("cuatro", 1) | |
| ).asObservable().doOnNext(new Action1<String>() { | |
| @Override | |
| public void call(String s) { | |
| System.out.println("onNext: " + s); | |
| } | |
| }).doOnCompleted(new Action0() { | |
| @Override | |
| public void call() { | |
| System.out.println("onCompleted"); | |
| } | |
| }).subscribe(testSubscriber); | |
| testSubscriber.awaitTerminalEvent(); | |
| testSubscriber.assertCompleted(); | |
| testSubscriber.assertNoErrors(); | |
| testSubscriber.assertReceivedOnNext(Arrays.asList("uno", "dos", "tres", "cuatro")); | |
| } | |
| private Observable<String> asyncObservable(String msg, int wait) { | |
| return Observable.just(msg).delay(wait, TimeUnit.SECONDS, Schedulers.newThread()); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output