Search in sources :

Example 66 with TestObserver

use of io.reactivex.rxjava3.observers.TestObserver in project RxJava by ReactiveX.

the class TestObserverTest method assertValuesOnlyThrowsWhenErrored.

@Test
public void assertValuesOnlyThrowsWhenErrored() {
    TestObserver<Integer> to = TestObserver.create();
    to.onSubscribe(Disposable.empty());
    to.onError(new TestException());
    try {
        to.assertValuesOnly();
        throw new RuntimeException();
    } catch (AssertionError ex) {
    // expected
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 67 with TestObserver

use of io.reactivex.rxjava3.observers.TestObserver in project RxJava by ReactiveX.

the class TestObserverTest method createDelegate.

@Test
public void createDelegate() {
    TestObserver<Integer> to1 = TestObserver.create();
    TestObserver<Integer> to = TestObserver.create(to1);
    assertFalse(to.hasSubscription());
    to.onSubscribe(Disposable.empty());
    assertTrue(to.hasSubscription());
    assertFalse(to.isDisposed());
    to.onNext(1);
    to.onError(new TestException());
    to.onComplete();
    to1.assertValue(1).assertError(TestException.class).assertComplete();
    to.dispose();
    assertTrue(to.isDisposed());
    try {
        to.assertNoValues();
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError exc) {
    // expected
    }
    try {
        to.assertValueCount(0);
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError exc) {
    // expected
    }
    to.assertValueSequence(Collections.singletonList(1));
    try {
        to.assertValueSequence(Collections.singletonList(2));
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError exc) {
    // expected
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 68 with TestObserver

use of io.reactivex.rxjava3.observers.TestObserver in project RxJava by ReactiveX.

the class TestObserverTest method errorDelegateThrows.

@Test
public void errorDelegateThrows() {
    TestObserver<Integer> to = new TestObserver<>(new Observer<Integer>() {

        @Override
        public void onSubscribe(Disposable d) {
        }

        @Override
        public void onNext(Integer value) {
        }

        @Override
        public void onError(Throwable e) {
            throw new TestException();
        }

        @Override
        public void onComplete() {
            throw new TestException();
        }
    });
    to.onSubscribe(Disposable.empty());
    try {
        to.onError(new IOException());
        throw new RuntimeException("Should have thrown!");
    } catch (TestException ex) {
        to.assertNotComplete().assertError(Throwable.class);
    }
}
Also used : ScalarDisposable(io.reactivex.rxjava3.internal.operators.observable.ObservableScalarXMap.ScalarDisposable) Disposable(io.reactivex.rxjava3.disposables.Disposable) TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException) Test(org.junit.Test)

Example 69 with TestObserver

use of io.reactivex.rxjava3.observers.TestObserver in project RxJava by ReactiveX.

the class TestObserverTest method assertError.

@Test
public void assertError() {
    TestObserver<Integer> to = TestObserver.create();
    try {
        to.assertError(TestException.class);
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError ex) {
    // expected
    }
    try {
        to.assertError(new TestException());
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError ex) {
    // expected
    }
    try {
        to.assertError(Functions.<Throwable>alwaysTrue());
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError ex) {
    // expected
    }
    try {
        to.assertSubscribed();
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError exc) {
    // expected
    }
    to.onSubscribe(Disposable.empty());
    to.assertSubscribed();
    to.assertNoErrors();
    TestException ex = new TestException("Forced failure");
    to.onError(ex);
    to.assertError(ex);
    to.assertError(TestException.class);
    to.assertError(Functions.<Throwable>alwaysTrue());
    to.assertError(new Predicate<Throwable>() {

        @Override
        public boolean test(Throwable t) throws Exception {
            return t.getMessage() != null && t.getMessage().contains("Forced");
        }
    });
    try {
        to.assertError(new RuntimeException());
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError exc) {
    // expected
    }
    try {
        to.assertError(IOException.class);
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError exc) {
    // expected
    }
    try {
        to.assertError(Functions.<Throwable>alwaysFalse());
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError exc) {
    // expected
    }
    try {
        to.assertNoErrors();
        throw new RuntimeException("Should have thrown");
    } catch (AssertionError exc) {
    // expected
    }
    to.assertValueCount(0);
    to.assertNoValues();
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException) Test(org.junit.Test)

Example 70 with TestObserver

use of io.reactivex.rxjava3.observers.TestObserver in project RxJava by ReactiveX.

the class TestObserverTest method assertErrorMultiple.

@Test
public void assertErrorMultiple() {
    TestObserver<Integer> to = new TestObserver<>();
    TestException e = new TestException();
    to.onError(e);
    to.onError(new TestException());
    try {
        to.assertError(TestException.class);
        throw new RuntimeException("Should have thrown!");
    } catch (AssertionError ex) {
    // expected
    }
    try {
        to.assertError(e);
        throw new RuntimeException("Should have thrown!");
    } catch (AssertionError ex) {
    // expected
    }
    try {
        to.assertError(Functions.<Throwable>alwaysTrue());
        throw new RuntimeException("Should have thrown!");
    } catch (AssertionError ex) {
    // expected
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)225 TestException (io.reactivex.rxjava3.exceptions.TestException)169 TestObserver (io.reactivex.rxjava3.observers.TestObserver)90 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)35 Disposable (io.reactivex.rxjava3.disposables.Disposable)34 CompletableSubject (io.reactivex.rxjava3.subjects.CompletableSubject)27 TargetApi (android.annotation.TargetApi)21 Observable (io.reactivex.rxjava3.core.Observable)19 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)19 IOException (java.io.IOException)19 Matchers.anyString (org.mockito.Matchers.anyString)16 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)13 Action (io.reactivex.rxjava3.functions.Action)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 InOrder (org.mockito.InOrder)7 io.reactivex.rxjava3.core (io.reactivex.rxjava3.core)6 RxJavaPlugins (io.reactivex.rxjava3.plugins.RxJavaPlugins)6 List (java.util.List)6 Assert (org.junit.Assert)6 Activity (android.app.Activity)5