From 9d46c8f3d1bece80f1292f4247d72863c1d74bf1 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Wed, 8 Feb 2017 12:05:02 +0100 Subject: [PATCH 1/2] BAEL-685 test for rx java --- rxjava/pom.xml | 17 +++ .../com/baeldung/rxjava/RxJavaTesting.java | 113 ++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 rxjava/src/test/java/com/baeldung/rxjava/RxJavaTesting.java diff --git a/rxjava/pom.xml b/rxjava/pom.xml index 6a351d98bd..f22e6a000a 100644 --- a/rxjava/pom.xml +++ b/rxjava/pom.xml @@ -31,11 +31,28 @@ junit ${junit.version} + + com.jayway.awaitility + awaitility + ${avaitility.version} + test + + + + org.hamcrest + hamcrest-all + ${hamcrest.version} + test + + + 4.12 1.2.5 + 1.7.0 + 1.3 \ No newline at end of file diff --git a/rxjava/src/test/java/com/baeldung/rxjava/RxJavaTesting.java b/rxjava/src/test/java/com/baeldung/rxjava/RxJavaTesting.java new file mode 100644 index 0000000000..67f6e1c2b8 --- /dev/null +++ b/rxjava/src/test/java/com/baeldung/rxjava/RxJavaTesting.java @@ -0,0 +1,113 @@ +package com.baeldung.rxjava; + +import org.junit.Test; +import rx.Observable; +import rx.observers.TestSubscriber; +import rx.schedulers.TestScheduler; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.TimeUnit; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.assertThat; + + +public class RxJavaTesting { + @Test + public void givenObservable_whenZip_shouldAssertBlockingInASameThread() { + //given + List letters = Arrays.asList("A", "B", "C", "D", "E"); + List results = new ArrayList<>(); + Observable observable = Observable + .from(letters) + .zipWith(Observable.range(1, Integer.MAX_VALUE), + (string, index) -> index + "-" + string); + + //when + observable.subscribe(results::add); + + //then + assertThat(results, notNullValue()); + assertThat(results, hasSize(5)); + assertThat(results, hasItems("1-A", "2-B", "3-C", "4-D", "5-E")); + } + + @Test + public void givenObservable_whenZip_shouldAssertOnTestSubscriber() { + //given + List letters = Arrays.asList("A", "B", "C", "D", "E"); + TestSubscriber subscriber = new TestSubscriber<>(); + + Observable observable = Observable + .from(letters) + .zipWith(Observable.range(1, Integer.MAX_VALUE), + ((string, index) -> index + "-" + string)); + + //when + observable.subscribe(subscriber); + + //then + subscriber.assertCompleted(); + subscriber.assertNoErrors(); + subscriber.assertValueCount(5); + assertThat(subscriber.getOnNextEvents(), hasItems("1-A", "2-B", "3-C", "4-D", "5-E")); + } + + @Test + public void givenTestObserver_whenExceptionWasThrowsOnObservable_observerShouldGetError() { + //given + List letters = Arrays.asList("A", "B", "C", "D", "E"); + TestSubscriber subscriber = new TestSubscriber<>(); + + + Observable observable = Observable + .from(letters) + .zipWith(Observable.range(1, Integer.MAX_VALUE), + ((string, index) -> index + "-" + string)) + .concatWith(Observable.error(new RuntimeException("error in Observable"))); + + //when + observable.subscribe(subscriber); + + //then + subscriber.assertError(RuntimeException.class); + subscriber.assertNotCompleted(); + } + + @Test + public void givenObservableThatEmitsEventPerSecond_whenUseAdvanceByTime_shouldEmitEventPerSecond() { + //given + List letters = Arrays.asList("A", "B", "C", "D", "E"); + TestScheduler scheduler = new TestScheduler(); + TestSubscriber subscriber = new TestSubscriber<>(); + Observable tick = Observable.interval(1, TimeUnit.SECONDS, scheduler); + + Observable observable = Observable.from(letters) + .zipWith(tick, (string, index) -> index + "-" + string); + + observable.subscribeOn(scheduler) + .subscribe(subscriber); + + //expect + subscriber.assertNoValues(); + subscriber.assertNotCompleted(); + + //when + scheduler.advanceTimeBy(1, TimeUnit.SECONDS); + + //then + subscriber.assertNoErrors(); + subscriber.assertValueCount(1); + subscriber.assertValues("0-A"); + + //when + scheduler.advanceTimeTo(6, TimeUnit.SECONDS); + subscriber.assertCompleted(); + subscriber.assertNoErrors(); + subscriber.assertValueCount(5); + assertThat(subscriber.getOnNextEvents(), hasItems("0-A", "1-B", "2-C", "3-D", "4-E")); + } +} + From 7d9c4b5bb9cc123d0479a73a218554bc7311e999 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Wed, 8 Feb 2017 13:43:23 +0100 Subject: [PATCH 2/2] BAEL-685 do not need avaitility lib --- rxjava/pom.xml | 8 -------- 1 file changed, 8 deletions(-) diff --git a/rxjava/pom.xml b/rxjava/pom.xml index f22e6a000a..b3936bf78d 100644 --- a/rxjava/pom.xml +++ b/rxjava/pom.xml @@ -31,13 +31,6 @@ junit ${junit.version} - - com.jayway.awaitility - awaitility - ${avaitility.version} - test - - org.hamcrest hamcrest-all @@ -51,7 +44,6 @@ 4.12 1.2.5 - 1.7.0 1.3