/** * This is testing a no-op path since it uses Schedulers.immediate() which will not do scheduling. */ @Test public void testObserveOn() { Observer<Integer> observer = TestHelper.mockObserver(); Observable.just(1, 2, 3).observeOn(ImmediateThinScheduler.INSTANCE).subscribe(observer); verify(observer, times(1)).onNext(1); verify(observer, times(1)).onNext(2); verify(observer, times(1)).onNext(3); verify(observer, times(1)).onComplete(); }
@Test // SPR-13225 public void headerWithNullValue() { HttpURLConnection urlConnection = mock(HttpURLConnection.class); HttpHeaders headers = new HttpHeaders(); headers.set("foo", null); SimpleBufferingClientHttpRequest.addHeaders(urlConnection, headers); verify(urlConnection, times(1)).addRequestProperty("foo", ""); }
@Test public void testListIterable() { Observable<String> o = Observable.fromIterable(Arrays.<String> asList("one", "two", "three")); Observer<String> observer = TestHelper.mockObserver(); o.subscribe(observer); verify(observer, times(1)).onNext("one"); verify(observer, times(1)).onNext("two"); verify(observer, times(1)).onNext("three"); verify(observer, Mockito.never()).onError(any(Throwable.class)); verify(observer, times(1)).onComplete(); }
public void testShutdownAndAwaitTermination_forcedShutDownInternal() throws Exception { ExecutorService service = mock(ExecutorService.class); when(service.awaitTermination(HALF_SECOND_NANOS, NANOSECONDS)) .thenReturn(false) .thenReturn(true); when(service.isTerminated()).thenReturn(true); assertTrue(shutdownAndAwaitTermination(service, 1L, SECONDS)); verify(service).shutdown(); verify(service, times(2)).awaitTermination(HALF_SECOND_NANOS, NANOSECONDS); verify(service).shutdownNow(); }
@Test public void testCast() { Observable<?> source = Observable.just(1, 2); Observable<Integer> observable = source.cast(Integer.class); Observer<Integer> observer = TestHelper.mockObserver(); observable.subscribe(observer); verify(observer, times(1)).onNext(1); verify(observer, times(1)).onNext(1); verify(observer, never()).onError( any(Throwable.class)); verify(observer, times(1)).onComplete(); }
public void testShutdownAndAwaitTermination_nonTerminationInternal() throws Exception { ExecutorService service = mock(ExecutorService.class); when(service.awaitTermination(HALF_SECOND_NANOS, NANOSECONDS)) .thenReturn(false) .thenReturn(false); assertFalse(shutdownAndAwaitTermination(service, 1L, SECONDS)); verify(service).shutdown(); verify(service, times(2)).awaitTermination(HALF_SECOND_NANOS, NANOSECONDS); verify(service).shutdownNow(); }
@Test public void testToMultimap() { Flowable<String> source = Flowable.just("a", "b", "cc", "dd"); Single<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFunc); Map<Integer, Collection<String>> expected = new HashMap<Integer, Collection<String>>(); expected.put(1, Arrays.asList("a", "b")); expected.put(2, Arrays.asList("cc", "dd")); mapped.subscribe(singleObserver); verify(singleObserver, never()).onError(any(Throwable.class)); verify(singleObserver, times(1)).onSuccess(expected); }
@Test public void sessionCompleted() { Runnable callback1 = Mockito.mock(Runnable.class); Runnable callback2 = Mockito.mock(Runnable.class); this.simpAttributes.registerDestructionCallback("name1", callback1); this.simpAttributes.registerDestructionCallback("name2", callback2); this.simpAttributes.sessionCompleted(); verify(callback1, times(1)).run(); verify(callback2, times(1)).run(); }
@Test public void testFirstOrElseWithPredicateOfNoneMatchingThePredicate() { Observable<String> src = Observable.just("a", "b", "c"); src.filter(IS_D).first("default").subscribe(wo); verify(wo, times(1)).onSuccess(anyString()); verify(wo, times(1)).onSuccess("default"); verify(wo, never()).onError(any(Throwable.class)); }
@Test public void testCombineLatest3TypesB() { Function3<String, Integer, int[], String> combineLatestFunction = getConcatStringIntegerIntArrayCombineLatestFunction(); /* define an Observer to receive aggregated events */ Observer<String> observer = TestHelper.mockObserver(); Observable<String> w = Observable.combineLatest(Observable.just("one"), Observable.just(2), Observable.just(new int[] { 4, 5, 6 }, new int[] { 7, 8 }), combineLatestFunction); w.subscribe(observer); verify(observer, never()).onError(any(Throwable.class)); verify(observer, times(1)).onComplete(); verify(observer, times(1)).onNext("one2[4, 5, 6]"); verify(observer, times(1)).onNext("one2[7, 8]"); }
@Test @SuppressWarnings({"rawtypes", "unchecked"}) public void cancelInactivityTasks() throws Exception { TcpConnection<byte[]> tcpConnection = getTcpConnection(); ScheduledFuture future = mock(ScheduledFuture.class); when(this.taskScheduler.scheduleWithFixedDelay(any(), eq(1L))).thenReturn(future); tcpConnection.onReadInactivity(mock(Runnable.class), 2L); tcpConnection.onWriteInactivity(mock(Runnable.class), 2L); this.webSocketHandlerCaptor.getValue().afterConnectionClosed(this.webSocketSession, CloseStatus.NORMAL); verify(future, times(2)).cancel(true); verifyNoMoreInteractions(future); }
@Test public void testSuccess() throws Exception { @SuppressWarnings("unchecked") Future<Object> future = mock(Future.class); Object value = new Object(); when(future.get()).thenReturn(value); Observer<Object> o = TestHelper.mockObserver(); TestObserver<Object> to = new TestObserver<Object>(o); Observable.fromFuture(future).subscribe(to); to.dispose(); verify(o, times(1)).onNext(value); verify(o, times(1)).onComplete(); verify(o, never()).onError(any(Throwable.class)); verify(future, never()).cancel(true); }
@Test public void testDematerialize1() { Observable<Notification<Integer>> notifications = Observable.just(1, 2).materialize(); Observable<Integer> dematerialize = notifications.dematerialize(); Observer<Integer> observer = TestHelper.mockObserver(); dematerialize.subscribe(observer); verify(observer, times(1)).onNext(1); verify(observer, times(1)).onNext(2); verify(observer, times(1)).onComplete(); verify(observer, never()).onError(any(Throwable.class)); }
@Test public void beanInstanceRetrievedAtEveryInvocation() { Method method = ReflectionUtils.findMethod( SampleEvents.class, "handleGenericString", GenericTestEvent.class); when(this.context.getBean("testBean")).thenReturn(this.sampleEvents); ApplicationListenerMethodAdapter listener = new ApplicationListenerMethodAdapter( "testBean", GenericTestEvent.class, method); listener.init(this.context, new EventExpressionEvaluator()); GenericTestEvent<String> event = createGenericTestEvent("test"); listener.onApplicationEvent(event); verify(this.sampleEvents, times(1)).handleGenericString(event); verify(this.context, times(1)).getBean("testBean"); listener.onApplicationEvent(event); verify(this.sampleEvents, times(2)).handleGenericString(event); verify(this.context, times(2)).getBean("testBean"); }
@Test public void testSkipTwoElements() { Observable<String> skip = Observable.just("one", "two", "three").skip(2); Observer<String> observer = TestHelper.mockObserver(); skip.subscribe(observer); verify(observer, never()).onNext("one"); verify(observer, never()).onNext("two"); verify(observer, times(1)).onNext("three"); verify(observer, never()).onError(any(Throwable.class)); verify(observer, times(1)).onComplete(); }
@Test public void testSuccess() throws Exception { @SuppressWarnings("unchecked") Future<Object> future = mock(Future.class); Object value = new Object(); when(future.get()).thenReturn(value); Subscriber<Object> subscriber = TestHelper.mockSubscriber(); TestSubscriber<Object> ts = new TestSubscriber<Object>(subscriber); Flowable.fromFuture(future).subscribe(ts); ts.dispose(); verify(subscriber, times(1)).onNext(value); verify(subscriber, times(1)).onComplete(); verify(subscriber, never()).onError(any(Throwable.class)); verify(future, never()).cancel(anyBoolean()); }
@Test public void testCountError() { Flowable<String> f = Flowable.error(new Callable<Throwable>() { @Override public Throwable call() { return new RuntimeException(); } }); f.count().subscribe(wo); verify(wo, never()).onSuccess(anyInt()); verify(wo, times(1)).onError(any(RuntimeException.class)); }