Skip to content

Instantly share code, notes, and snippets.

@keima
Created December 4, 2015 06:31
Show Gist options
  • Select an option

  • Save keima/de360841402fbf9c82df to your computer and use it in GitHub Desktop.

Select an option

Save keima/de360841402fbf9c82df to your computer and use it in GitHub Desktop.
RxJavaでの非同期テストの書き方練習
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());
}
}
@keima
Copy link
Author

keima commented Dec 4, 2015

Output

Start.
onNext: uno
onNext: dos
onNext: tres
onNext: cuatro
onCompleted

Process finished with exit code 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment