@Test public void testDefer() throws Throwable { Supplier<Observable<String>> factory = mock(Supplier.class); Observable<String> firstObservable = Observable.just("one", "two"); Observable<String> secondObservable = Observable.just("three", "four"); when(factory.get()).thenReturn(firstObservable, secondObservable); Observable<String> deferred = Observable.defer(factory); verifyZeroInteractions(factory); Subscriber<String> firstObserver = TestHelper.mockSubscriber(); deferred.subscribe(firstObserver); verify(factory, times(1)).get(); verify(firstObserver, times(1)).onNext("one"); verify(firstObserver, times(1)).onNext("two"); verify(firstObserver, times(0)).onNext("three"); verify(firstObserver, times(0)).onNext("four"); verify(firstObserver, times(1)).onComplete(); Subscriber<String> secondObserver = TestHelper.mockSubscriber(); deferred.subscribe(secondObserver); verify(factory, times(2)).get(); verify(secondObserver, times(0)).onNext("one"); verify(secondObserver, times(0)).onNext("two"); verify(secondObserver, times(1)).onNext("three"); verify(secondObserver, times(1)).onNext("four"); verify(secondObserver, times(1)).onComplete(); }
@Test public void testIgnoreElements() { Observable<Integer> observable = Observable.just(1, 2, 3).ignoreElements(); Subscriber<Object> observer = TestHelper.mockSubscriber(); observable.subscribe(observer); verify(observer, never()).onNext(any(Integer.class)); verify(observer, never()).onError(any(Throwable.class)); verify(observer, times(1)).onComplete(); }
@Test public void testContainsWithEmptyObservable() { Observable<Boolean> observable = Observable.<String>empty().contains("a"); Subscriber<Object> observer = TestHelper.mockSubscriber(); observable.subscribe(observer); verify(observer, times(1)).onNext(false); verify(observer, never()).onNext(true); verify(observer, never()).onError(org.mockito.Matchers.any(Throwable.class)); verify(observer, times(1)).onComplete(); }
@Test public void testMaterializeDematerializeChaining() { Observable<Integer> obs = Observable.just(1); Observable<Integer> chained = obs.materialize().dematerialize(); Subscriber<Integer> observer = TestHelper.mockSubscriber(); chained.subscribe(observer); verify(observer, times(1)).onNext(1); verify(observer, times(1)).onComplete(); verify(observer, times(0)).onError(any(Throwable.class)); }
@Test @Ignore("null values are not allowed") public void testContainsWithNull() { Observable<Boolean> observable = Observable.just("a", "b", null).contains(null); Subscriber<Object> observer = TestHelper.mockSubscriber(); observable.subscribe(observer); verify(observer, times(1)).onNext(true); verify(observer, never()).onNext(false); verify(observer, never()).onError(org.mockito.Matchers.any(Throwable.class)); verify(observer, times(1)).onComplete(); }
@Test public void testContainsWithInexistence() { Observable<Boolean> observable = Observable.just("a", "b").contains("c"); // FIXME null values are not allowed, removed Subscriber<Object> observer = TestHelper.mockSubscriber(); observable.subscribe(observer); verify(observer, times(1)).onNext(false); verify(observer, never()).onNext(true); verify(observer, never()).onError(org.mockito.Matchers.any(Throwable.class)); verify(observer, times(1)).onComplete(); }
@Test public void testContains() { Observable<Boolean> observable = Observable.just("a", "b", "c").contains("b"); // FIXME nulls not allowed, changed to "c" Subscriber<Boolean> observer = TestHelper.mockSubscriber(); observable.subscribe(observer); verify(observer, times(1)).onNext(true); verify(observer, never()).onNext(false); verify(observer, never()).onError(org.mockito.Matchers.any(Throwable.class)); verify(observer, times(1)).onComplete(); }
@Test public void testCreate() { Observable<String> observable = Observable.just("one", "two", "three"); Subscriber<String> observer = TestHelper.mockSubscriber(); observable.subscribe(observer); verify(observer, times(1)).onNext("one"); verify(observer, times(1)).onNext("two"); verify(observer, times(1)).onNext("three"); verify(observer, never()).onError(any(Throwable.class)); verify(observer, times(1)).onComplete(); }
@Test public void testOfType() { Observable<String> observable = Observable.just(1, "abc", false, 2L).ofType(String.class); Subscriber<Object> observer = TestHelper.mockSubscriber(); observable.subscribe(observer); verify(observer, never()).onNext(1); verify(observer, times(1)).onNext("abc"); verify(observer, never()).onNext(false); verify(observer, never()).onNext(2L); verify(observer, never()).onError(org.mockito.Matchers.any(Throwable.class)); verify(observer, times(1)).onComplete(); }
@Test public void testJustWithScheduler() { TestScheduler scheduler = new TestScheduler(); Observable<Integer> observable = Observable.fromArray(1, 2).subscribeOn(scheduler); Subscriber<Integer> observer = TestHelper.mockSubscriber(); observable.subscribe(observer); scheduler.advanceTimeBy(1, TimeUnit.MILLISECONDS); InOrder inOrder = inOrder(observer); inOrder.verify(observer, times(1)).onNext(1); inOrder.verify(observer, times(1)).onNext(2); inOrder.verify(observer, times(1)).onComplete(); inOrder.verifyNoMoreInteractions(); }
@Ignore // FIXME throwing is not allowed from the create?! @Test public void testOnSubscribeFails() { Subscriber<String> observer = TestHelper.mockSubscriber(); final RuntimeException re = new RuntimeException("bad impl"); Observable<String> o = Observable.create( s -> { throw re; }); o.subscribe(observer); verify(observer, times(0)).onNext(anyString()); verify(observer, times(0)).onComplete(); verify(observer, times(1)).onError(re); }
@Test public void testHiding() { PublishSubject<Integer> src = PublishSubject.create(); Observable<Integer> dst = src.asObservable(); assertFalse(dst instanceof PublishSubject); Subscriber<Object> o = TestHelper.mockSubscriber(); dst.subscribe(o); src.onNext(1); src.onComplete(); verify(o).onNext(1); verify(o).onComplete(); verify(o, never()).onError(any(Throwable.class)); }
@Test public void testOfTypeWithPolymorphism() { ArrayList<Integer> l1 = new ArrayList<>(); l1.add(1); LinkedList<Integer> l2 = new LinkedList<>(); l2.add(2); @SuppressWarnings("rawtypes") Observable<List> observable = Observable.<Object>just(l1, l2, "123").ofType(List.class); Subscriber<Object> observer = TestHelper.mockSubscriber(); observable.subscribe(observer); verify(observer, times(1)).onNext(l1); verify(observer, times(1)).onNext(l2); verify(observer, never()).onNext("123"); verify(observer, never()).onError(org.mockito.Matchers.any(Throwable.class)); verify(observer, times(1)).onComplete(); }
@Before public void before() { observer = TestHelper.mockSubscriber(); }
@Before public void before() { w = TestHelper.mockSubscriber(); }