OperationSingle { public static <T> OnSubscribeFunc<T> single( final Observable<? extends T> source) { return single(source, false, null); }  static OnSubscribeFunc<T> single(
            final Observable<? extends T> source); static OnSubscribeFunc<T> singleOrDefault(
            final Observable<? extends T> source, final T defaultValue);  }
@Test public void testSingle() { Observable<Integer> observable = Observable.from(1).single(); @SuppressWarnings("unchecked") Observer<Integer> observer = (Observer<Integer>) mock(Observer.class); observable.subscribe(observer); InOrder inOrder = inOrder(observer); inOrder.verify(observer, times(1)).onNext(1); inOrder.verify(observer, times(1)).onCompleted(); inOrder.verifyNoMoreInteractions(); }
@Test public void testSingleWithTooManyElements() { Observable<Integer> observable = Observable.from(1, 2).single(); @SuppressWarnings("unchecked") Observer<Integer> observer = (Observer<Integer>) mock(Observer.class); observable.subscribe(observer); InOrder inOrder = inOrder(observer); inOrder.verify(observer, times(1)).onError( isA(IllegalArgumentException.class)); inOrder.verifyNoMoreInteractions(); }
@Test public void testSingleWithEmpty() { Observable<Integer> observable = Observable.<Integer> empty().single(); @SuppressWarnings("unchecked") Observer<Integer> observer = (Observer<Integer>) mock(Observer.class); observable.subscribe(observer); InOrder inOrder = inOrder(observer); inOrder.verify(observer, times(1)).onError( isA(IllegalArgumentException.class)); inOrder.verifyNoMoreInteractions(); }
@Test public void testSingleWithPredicate() { Observable<Integer> observable = Observable.from(1, 2).single( new Func1<Integer, Boolean>() { @Override public Boolean call(Integer t1) { return t1 % 2 == 0; } }); @SuppressWarnings("unchecked") Observer<Integer> observer = (Observer<Integer>) mock(Observer.class); observable.subscribe(observer); InOrder inOrder = inOrder(observer); inOrder.verify(observer, times(1)).onNext(2); inOrder.verify(observer, times(1)).onCompleted(); inOrder.verifyNoMoreInteractions(); }
@Test public void testSingleWithPredicateAndTooManyElements() { Observable<Integer> observable = Observable.from(1, 2, 3, 4).single( new Func1<Integer, Boolean>() { @Override public Boolean call(Integer t1) { return t1 % 2 == 0; } }); @SuppressWarnings("unchecked") Observer<Integer> observer = (Observer<Integer>) mock(Observer.class); observable.subscribe(observer); InOrder inOrder = inOrder(observer); inOrder.verify(observer, times(1)).onError( isA(IllegalArgumentException.class)); inOrder.verifyNoMoreInteractions(); }
@Test public void testSingleWithPredicateAndEmpty() { Observable<Integer> observable = Observable.from(1).single( new Func1<Integer, Boolean>() { @Override public Boolean call(Integer t1) { return t1 % 2 == 0; } }); @SuppressWarnings("unchecked") Observer<Integer> observer = (Observer<Integer>) mock(Observer.class); observable.subscribe(observer); InOrder inOrder = inOrder(observer); inOrder.verify(observer, times(1)).onError( isA(IllegalArgumentException.class)); inOrder.verifyNoMoreInteractions(); }