private static void verifyObserver(Observer<Integer> mock, int numSubscriptions, int numItemsExpected, Throwable error) { verify(mock, times(numItemsExpected)).onNext((Integer) notNull()); verify(mock, times(numSubscriptions)).onError(error); verifyNoMoreInteractions(mock); }
private static void verifyObserverMock(Observer<Integer> mock, int numSubscriptions, int numItemsExpected) { verify(mock, times(numItemsExpected)).onNext((Integer) notNull()); verify(mock, times(numSubscriptions)).onComplete(); verifyNoMoreInteractions(mock); }
private static void verifyObserver(Subscriber<Integer> mock, int numSubscriptions, int numItemsExpected, Throwable error) { verify(mock, times(numItemsExpected)).onNext((Integer) notNull()); verify(mock, times(numSubscriptions)).onError(error); verifyNoMoreInteractions(mock); }
private static void verifyObserverMock(Subscriber<Integer> mock, int numSubscriptions, int numItemsExpected) { verify(mock, times(numItemsExpected)).onNext((Integer) notNull()); verify(mock, times(numSubscriptions)).onComplete(); verifyNoMoreInteractions(mock); }
@Test public void testTakeDoesntLeakErrors() { Observable<String> source = Observable.unsafeCreate(new ObservableSource<String>() { @Override public void subscribe(Observer<? super String> observer) { observer.onSubscribe(Disposables.empty()); observer.onNext("one"); observer.onError(new Throwable("test failed")); } }); Observer<String> observer = TestHelper.mockObserver(); source.take(1).subscribe(observer); verify(observer).onSubscribe((Disposable)notNull()); verify(observer, times(1)).onNext("one"); // even though onError is called we take(1) so shouldn't see it verify(observer, never()).onError(any(Throwable.class)); verify(observer, times(1)).onComplete(); verifyNoMoreInteractions(observer); }
@Test public void webSocketConnectionEstablished() throws Exception { connect().afterConnectionEstablished(this.webSocketSession); verify(this.stompSession).afterConnected(notNull()); }
@Test public void testOnErrorFromNotificationHandler() { Observer<String> observer = TestHelper.mockObserver(); Observable<String> origin = Observable.unsafeCreate(new FuncWithErrors(2)); origin.retryWhen(new Function<Observable<? extends Throwable>, Observable<?>>() { @Override public Observable<?> apply(Observable<? extends Throwable> t1) { return Observable.error(new RuntimeException()); } }).subscribe(observer); InOrder inOrder = inOrder(observer); inOrder.verify(observer).onSubscribe((Disposable)notNull()); inOrder.verify(observer, never()).onNext("beginningEveryTime"); inOrder.verify(observer, never()).onNext("onSuccessOnly"); inOrder.verify(observer, never()).onComplete(); inOrder.verify(observer, times(1)).onError(any(RuntimeException.class)); inOrder.verifyNoMoreInteractions(); }
@Test public void testOnErrorFromNotificationHandler() { Subscriber<String> subscriber = TestHelper.mockSubscriber(); Flowable<String> origin = Flowable.unsafeCreate(new FuncWithErrors(2)); origin.retryWhen(new Function<Flowable<? extends Throwable>, Flowable<Object>>() { @Override public Flowable<Object> apply(Flowable<? extends Throwable> t1) { return Flowable.error(new RuntimeException()); } }).subscribe(subscriber); InOrder inOrder = inOrder(subscriber); inOrder.verify(subscriber).onSubscribe((Subscription)notNull()); inOrder.verify(subscriber, never()).onNext("beginningEveryTime"); inOrder.verify(subscriber, never()).onNext("onSuccessOnly"); inOrder.verify(subscriber, never()).onComplete(); inOrder.verify(subscriber, times(1)).onError(any(RuntimeException.class)); inOrder.verifyNoMoreInteractions(); }
@Test public void testUnsubscribeAfterTake() { TestFlowableFunc f = new TestFlowableFunc("one", "two", "three"); Flowable<String> w = Flowable.unsafeCreate(f); Subscriber<String> subscriber = TestHelper.mockSubscriber(); Flowable<String> take = w.take(1); take.subscribe(subscriber); // wait for the Flowable to complete try { f.t.join(); } catch (Throwable e) { e.printStackTrace(); fail(e.getMessage()); } System.out.println("TestFlowable thread finished"); verify(subscriber).onSubscribe((Subscription)notNull()); verify(subscriber, times(1)).onNext("one"); verify(subscriber, never()).onNext("two"); verify(subscriber, never()).onNext("three"); verify(subscriber, times(1)).onComplete(); // FIXME no longer assertable // verify(s, times(1)).unsubscribe(); verifyNoMoreInteractions(subscriber); }
@Test public void testUnsubscribeAfterTake() { TestObservableFunc f = new TestObservableFunc("one", "two", "three"); Observable<String> w = Observable.unsafeCreate(f); Observer<String> observer = TestHelper.mockObserver(); Observable<String> take = w.take(1); take.subscribe(observer); // wait for the Observable to complete try { f.t.join(); } catch (Throwable e) { e.printStackTrace(); fail(e.getMessage()); } System.out.println("TestObservable thread finished"); verify(observer).onSubscribe((Disposable)notNull()); verify(observer, times(1)).onNext("one"); verify(observer, never()).onNext("two"); verify(observer, never()).onNext("three"); verify(observer, times(1)).onComplete(); // FIXME no longer assertable // verify(s, times(1)).unsubscribe(); verifyNoMoreInteractions(observer); }
@Test public void testCompletedAfterError() { ReplayProcessor<String> processor = ReplayProcessor.create(); Subscriber<String> subscriber = TestHelper.mockSubscriber(); processor.onNext("one"); processor.onError(testException); processor.onNext("two"); processor.onComplete(); processor.onError(new RuntimeException()); processor.subscribe(subscriber); verify(subscriber).onSubscribe((Subscription)notNull()); verify(subscriber, times(1)).onNext("one"); verify(subscriber, times(1)).onError(testException); verifyNoMoreInteractions(subscriber); }
@Test public void testOnCompletedFromNotificationHandler() { Observer<String> observer = TestHelper.mockObserver(); Observable<String> origin = Observable.unsafeCreate(new FuncWithErrors(1)); TestObserver<String> to = new TestObserver<String>(observer); origin.retryWhen(new Function<Observable<? extends Throwable>, Observable<?>>() { @Override public Observable<?> apply(Observable<? extends Throwable> t1) { return Observable.empty(); } }).subscribe(to); InOrder inOrder = inOrder(observer); inOrder.verify(observer).onSubscribe((Disposable)notNull()); inOrder.verify(observer, never()).onNext("beginningEveryTime"); inOrder.verify(observer, never()).onNext("onSuccessOnly"); inOrder.verify(observer, times(1)).onComplete(); inOrder.verify(observer, never()).onError(any(Exception.class)); inOrder.verifyNoMoreInteractions(); }
@Test public void testOnCompletedFromNotificationHandler() { Subscriber<String> subscriber = TestHelper.mockSubscriber(); Flowable<String> origin = Flowable.unsafeCreate(new FuncWithErrors(1)); TestSubscriber<String> ts = new TestSubscriber<String>(subscriber); origin.retryWhen(new Function<Flowable<? extends Throwable>, Flowable<Object>>() { @Override public Flowable<Object> apply(Flowable<? extends Throwable> t1) { return Flowable.empty(); } }).subscribe(ts); InOrder inOrder = inOrder(subscriber); inOrder.verify(subscriber).onSubscribe((Subscription)notNull()); inOrder.verify(subscriber, never()).onNext("beginningEveryTime"); inOrder.verify(subscriber, never()).onNext("onSuccessOnly"); inOrder.verify(subscriber, times(1)).onComplete(); inOrder.verify(subscriber, never()).onError(any(Exception.class)); inOrder.verifyNoMoreInteractions(); }
@Test @SuppressWarnings({ "unchecked", "rawtypes" }) public void receiptNotReceived() { TaskScheduler taskScheduler = mock(TaskScheduler.class); this.session.afterConnected(this.connection); this.session.setTaskScheduler(taskScheduler); AtomicReference<Boolean> notReceived = new AtomicReference<>(); ScheduledFuture future = mock(ScheduledFuture.class); when(taskScheduler.schedule(any(Runnable.class), any(Date.class))).thenReturn(future); StompHeaders headers = new StompHeaders(); headers.setDestination("/topic/foo"); headers.setReceipt("my-receipt"); Receiptable receiptable = this.session.send(headers, "payload"); receiptable.addReceiptLostTask(() -> notReceived.set(true)); ArgumentCaptor<Runnable> taskCaptor = ArgumentCaptor.forClass(Runnable.class); verify(taskScheduler).schedule(taskCaptor.capture(), (Date) notNull()); Runnable scheduledTask = taskCaptor.getValue(); assertNotNull(scheduledTask); assertNull(notReceived.get()); scheduledTask.run(); assertTrue(notReceived.get()); verify(future).cancel(true); verifyNoMoreInteractions(future); }
@Test public void testCompletedAfterError() { ReplaySubject<String> subject = ReplaySubject.create(); Observer<String> observer = TestHelper.mockObserver(); subject.onNext("one"); subject.onError(testException); subject.onNext("two"); subject.onComplete(); subject.onError(new RuntimeException()); subject.subscribe(observer); verify(observer).onSubscribe((Disposable)notNull()); verify(observer, times(1)).onNext("one"); verify(observer, times(1)).onError(testException); verifyNoMoreInteractions(observer); }
@Test public void testTakeDoesntLeakErrors() { List<Throwable> errors = TestHelper.trackPluginErrors(); try { Flowable<String> source = Flowable.unsafeCreate(new Publisher<String>() { @Override public void subscribe(Subscriber<? super String> subscriber) { subscriber.onSubscribe(new BooleanSubscription()); subscriber.onNext("one"); subscriber.onError(new Throwable("test failed")); } }); Subscriber<String> subscriber = TestHelper.mockSubscriber(); source.take(1).subscribe(subscriber); verify(subscriber).onSubscribe((Subscription)notNull()); verify(subscriber, times(1)).onNext("one"); // even though onError is called we take(1) so shouldn't see it verify(subscriber, never()).onError(any(Throwable.class)); verify(subscriber, times(1)).onComplete(); verifyNoMoreInteractions(subscriber); TestHelper.assertUndeliverable(errors, 0, Throwable.class, "test failed"); } finally { RxJavaPlugins.reset(); } }
verify(sourceCompleted, times(1)).run(); verify(mockScheduler, times(1)).createWorker(); verify(spiedWorker, times(1)).schedule((Runnable)notNull()); verifyObserverMock(mockObserverBeforeConnect, 2, 6); verifyObserverMock(mockObserverAfterConnect, 2, 6);
verify(spiedWorker, times(1)).schedule((Runnable)notNull()); verify(sourceNext, times(1)).accept(1); verify(sourceError, times(1)).accept(illegalArgumentException);
inOrderD.verify(observerD).onComplete(); verify(observerA).onSubscribe((Subscription)notNull()); verify(observerB).onSubscribe((Subscription)notNull()); verify(observerC).onSubscribe((Subscription)notNull()); verify(observerD).onSubscribe((Subscription)notNull()); Mockito.verifyNoMoreInteractions(observerA); Mockito.verifyNoMoreInteractions(observerB);
inOrderD.verify(observerD).onComplete(); verify(observerA).onSubscribe((Disposable)notNull()); verify(observerB).onSubscribe((Disposable)notNull()); verify(observerC).onSubscribe((Disposable)notNull()); verify(observerD).onSubscribe((Disposable)notNull()); Mockito.verifyNoMoreInteractions(observerA); Mockito.verifyNoMoreInteractions(observerB);