@Test public void testReadBytes_whenReturnsLessThanZeroWithInvalidLength_doesNotThrow() throws IOException { InputStream is = ContentLengthInputStream.obtain(wrapped, "invalid_length"); when(wrapped.read(any(byte[].class), anyInt(), anyInt())).thenReturn(-1); //noinspection ResultOfMethodCallIgnored is.read(new byte[10], 0, 0); }
/** * Get the {@code exception} that the supplied {@linkplain FrameworkMethod * test method} is expected to throw. * <p>Supports JUnit's {@link Test#expected() @Test(expected=...)} annotation. * <p>Can be overridden by subclasses. * @return the expected exception, or {@code null} if none was specified */ @Nullable protected Class<? extends Throwable> getExpectedException(FrameworkMethod frameworkMethod) { Test test = frameworkMethod.getAnnotation(Test.class); return (test != null && test.expected() != Test.None.class ? test.expected() : null); }
/** * Retrieve the configured JUnit {@code timeout} from the {@link Test @Test} * annotation on the supplied {@linkplain FrameworkMethod test method}. * @return the timeout, or {@code 0} if none was specified */ protected long getJUnitTimeout(FrameworkMethod frameworkMethod) { Test test = frameworkMethod.getAnnotation(Test.class); return (test != null && test.timeout() > 0 ? test.timeout() : 0); }
@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 toEntityListClass() { Mono<ResponseEntity<List<String>>> result = Mono.just(new ResponseEntity<>(singletonList("foo"), HttpStatus.OK)); when(mockResponse.toEntityList(String.class)).thenReturn(result); assertSame(result, wrapper.toEntityList(String.class)); }
@SuppressWarnings("unchecked") @Test public void shouldCallOnError() throws Exception { Callable<Object> func = mock(Callable.class); Throwable throwable = new IllegalStateException("Test exception"); when(func.call()).thenThrow(throwable); Observable<Object> fromCallableObservable = Observable.fromCallable(func); Observer<Object> observer = TestHelper.mockObserver(); fromCallableObservable.subscribe(observer); verify(observer, never()).onNext(any()); verify(observer, never()).onComplete(); verify(observer).onError(throwable); }
@Test @SuppressWarnings("unchecked") public void cookies() { MultiValueMap<String, HttpCookie> cookies = mock(MultiValueMap.class); when(mockRequest.cookies()).thenReturn(cookies); assertSame(cookies, wrapper.cookies()); }
@SuppressWarnings("unchecked") @Test public void shouldNotInvokeFuncUntilSubscription() throws Exception { Callable<Object> func = mock(Callable.class); when(func.call()).thenReturn(new Object()); Single<Object> fromCallableSingle = Single.fromCallable(func); verifyZeroInteractions(func); fromCallableSingle.subscribe(); verify(func).call(); }
@Test public void testCancelledBeforeSubscribe() throws Exception { @SuppressWarnings("unchecked") Future<Object> future = mock(Future.class); CancellationException e = new CancellationException("unit test synthetic cancellation"); when(future.get()).thenThrow(e); Observer<Object> o = TestHelper.mockObserver(); TestObserver<Object> to = new TestObserver<Object>(o); to.dispose(); Observable.fromFuture(future).subscribe(to); to.assertNoErrors(); to.assertNotComplete(); }
@Test public void testPassesArgb8888ToStrategyAsConfigForRequestsWithNullConfigsOnGetDirty() { LruPoolStrategy strategy = mock(LruPoolStrategy.class); LruBitmapPool pool = new LruBitmapPool(100, strategy, ALLOWED_CONFIGS); Bitmap expected = createMutableBitmap(); when(strategy.get(anyInt(), anyInt(), eq(Bitmap.Config.ARGB_8888))).thenReturn(expected); Bitmap result = pool.getDirty(100, 100, null); assertEquals(expected, result); }
@Test(timeout = 2000) public void bufferWithSizeSkipTake1() { Observable<Integer> source = Observable.just(1).repeat(); Observable<List<Integer>> result = source.buffer(2, 3).take(1); Observer<Object> o = TestHelper.mockObserver(); result.subscribe(o); verify(o).onNext(Arrays.asList(1, 1)); verify(o).onComplete(); verify(o, never()).onError(any(Throwable.class)); }
@Test public void testReturnsParserToPoolWhenParserThrows() { when(parser.parseHeader()).thenThrow(new RuntimeException("Test")); try { decoder.decode(ByteBuffer.allocate(10), 100, 100, options); fail("Failed to receive expected exception"); } catch (RuntimeException e) { // Expected. } verify(parserPool).release(eq(parser)); }
@Test public void testSingleOrDefault() { Single<Integer> single = Flowable.just(1).single(2); SingleObserver<Integer> observer = TestHelper.mockSingleObserver(); single.subscribe(observer); InOrder inOrder = inOrder(observer); inOrder.verify(observer, times(1)).onSuccess(1); inOrder.verifyNoMoreInteractions(); }
@Test @Ignore("Null values no longer allowed") public void testDistinctOfSourceWithExceptionsFromKeySelector() { Observable<String> src = Observable.just("a", "b", null, "c"); src.distinct(TO_UPPER_WITH_EXCEPTION).subscribe(w); InOrder inOrder = inOrder(w); inOrder.verify(w, times(1)).onNext("a"); inOrder.verify(w, times(1)).onNext("b"); inOrder.verify(w, times(1)).onError(any(NullPointerException.class)); inOrder.verify(w, never()).onNext(anyString()); inOrder.verify(w, never()).onComplete(); }
@Test public void testConcat() { Observer<String> observer = TestHelper.mockObserver(); final String[] o = { "1", "3", "5", "7" }; final String[] e = { "2", "4", "6" }; final Observable<String> odds = Observable.fromArray(o); final Observable<String> even = Observable.fromArray(e); Observable<String> concat = Observable.concat(odds, even); concat.subscribe(observer); verify(observer, times(7)).onNext(anyString()); }
@Test public void testSkipError() { Flowable<Integer> src = Flowable.just(1, 2, 42, 5, 3, 1); src.skipWhile(LESS_THAN_FIVE).subscribe(w); InOrder inOrder = inOrder(w); inOrder.verify(w, never()).onNext(anyInt()); inOrder.verify(w, never()).onComplete(); inOrder.verify(w, times(1)).onError(any(RuntimeException.class)); }
@Test public void testSkipAndCountGaplessBuffers() { Observable<String> source = Observable.just("one", "two", "three", "four", "five"); Observable<List<String>> buffered = source.buffer(3, 3); buffered.subscribe(observer); InOrder inOrder = Mockito.inOrder(observer); inOrder.verify(observer, Mockito.times(1)).onNext(list("one", "two", "three")); inOrder.verify(observer, Mockito.times(1)).onNext(list("four", "five")); inOrder.verify(observer, Mockito.never()).onNext(Mockito.<String>anyList()); inOrder.verify(observer, Mockito.never()).onError(Mockito.any(Throwable.class)); inOrder.verify(observer, Mockito.times(1)).onComplete(); }