/** * The error from the user provided Observable is handled by the subscribe try/catch because this * is synchronous * * <p>Result: Passes */ @Test public void testCustomObservableWithErrorInObservableSynchronous() { final AtomicInteger count = new AtomicInteger(); final AtomicReference<Throwable> error = new AtomicReference<>(); // FIXME custom built??? Observable.just("1", "2") .concatWith(Observable.error(() -> new NumberFormatException())) .subscribe( new Observer<String>() { @Override public void onComplete() { System.out.println("completed"); } @Override public void onError(Throwable e) { error.set(e); System.out.println("error"); e.printStackTrace(); } @Override public void onNext(String v) { System.out.println(v); count.incrementAndGet(); } }); assertEquals(2, count.get()); assertNotNull(error.get()); if (!(error.get() instanceof NumberFormatException)) { fail("It should be a NumberFormatException"); } }
@Test public void testAssertError() { RuntimeException e = new RuntimeException("Oops"); TestSubscriber<Object> subscriber = new TestSubscriber<Object>(); Observable.error(e).subscribe(subscriber); subscriber.assertError(e); }
@Test public void testCountError() { Observable<String> o = Observable.error(() -> new RuntimeException()); o.count().subscribe(w); verify(w, never()).onNext(anyInt()); verify(w, never()).onComplete(); verify(w, times(1)).onError(any(RuntimeException.class)); }
/** * https://github.com/ReactiveX/RxJava/issues/198 * * <p>Rx Design Guidelines 5.2 * * <p>"when calling the Subscribe method that only has an onNext argument, the OnError behavior * will be to rethrow the exception on the thread that the message comes out from the Observable. * The OnCompleted behavior in this case is to do nothing." */ @Test @Ignore("Subscribers can't throw") public void testErrorThrownWithoutErrorHandlerSynchronous() { try { Observable.error(new RuntimeException("failure")).subscribe(); fail("expected exception"); } catch (Throwable e) { assertEquals("failure", e.getMessage()); } }
@Test public void testErrorThrownIssue1685() { Subject<Object, Object> subject = ReplaySubject.create(); Observable.error(new RuntimeException("oops")) .materialize() .delay(1, TimeUnit.SECONDS) .dematerialize() .subscribe(subject); subject.subscribe(); subject.materialize().toBlocking().first(); System.out.println("Done"); }
@Test public void testDoOnError() { final AtomicReference<Throwable> r = new AtomicReference<Throwable>(); Throwable t = null; try { Observable.<String>error(new RuntimeException("an error")) .doOnError( new Action1<Throwable>() { @Override public void call(Throwable v) { r.set(v); } }) .toBlockingObservable() .single(); fail("expected exception, not a return value"); } catch (Throwable e) { t = e; } assertNotNull(t); assertEquals(t, r.get()); }
@Test(expected = NullPointerException.class) public void testForEachWithNull() { Observable.error(new Exception("boo")) // .forEach(null); }