Class TestObserver<T>
java.lang.Object
io.reactivex.rxjava4.observers.BaseTestConsumer<T, TestObserver<T>>
io.reactivex.rxjava4.observers.TestObserver<T>
- Type Parameters:
T- the value type
- All Implemented Interfaces:
CompletableObserver, MaybeObserver<T>, Observer<T>, SingleObserver<T>, Disposable, AutoCloseable
public class TestObserver<T>
extends BaseTestConsumer<T, TestObserver<T>>
implements Observer<T>, Disposable, MaybeObserver<T>, SingleObserver<T>, CompletableObserver
An
Observer, MaybeObserver, SingleObserver and
CompletableObserver composite that can record events from
Observables, Maybes, Singles and Completables
and allows making assertions about them.
You can override the onSubscribe(Disposable), onNext(Object), onError(Throwable),
onComplete() and onSuccess(Object) methods but not the others (this is by design).
The TestObserver implements Disposable for convenience where dispose calls cancel.
- See Also:
-
Field Summary
Fields inherited from class BaseTestConsumer
checkSubscriptionOnce, completions, done, errors, lastThread, tag, timeout, valuesModifier and TypeFieldDescriptionprotected booleanprotected longThe number of completions.protected final CountDownLatchThe latch that indicates an onError or onComplete has been called.The list of errors received.protected ThreadThe last thread seen by the observer.protected CharSequenceThe optional tag associated with this test consumer.protected booleanIndicates that one of theawaitXmethod has timed out.The list of values received. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a non-forwarding TestObserver.TestObserver(@NonNull Observer<? super T> downstream) Constructs a forwardingTestObserver. -
Method Summary
Modifier and TypeMethodDescriptionprotected final @NonNull TestObserver<T> Assert that theonSubscribe(Disposable)method was called exactly once.static <T> @NonNull TestObserver<T> create()Constructs a non-forwardingTestObserver.static <T> @NonNull TestObserver<T> Constructs a forwardingTestObserver.final voiddispose()Cancel/dispose this test consumer.final booleanReturns true if thisTestObserverreceived a subscription.final booleanReturns true if this test consumer was cancelled/disposed.voidNotifies theObserverthat theObservablehas finished sending push-based notifications.voidNotifies theObserverthat theObservablehas experienced an error condition.voidProvides theObserverwith a new item to observe.voidProvides theObserverwith the means of cancelling (disposing) the connection (channel) with theObservablein both synchronous (from withinObserver.onNext(Object)) and asynchronous manner.voidNotifies theMaybeObserverwith one item and that theMaybehas finished sending push-based notifications.Methods inherited from class BaseTestConsumer
assertComplete, assertEmpty, assertError, assertError, assertError, assertFailure, assertNoErrors, assertNotComplete, assertNoValues, assertResult, assertValue, assertValue, assertValueAt, assertValueAt, assertValueCount, assertValues, assertValueSequence, assertValuesOnly, await, await, awaitCount, awaitDone, fail, valueAndClass, values, withTagModifier and TypeMethodDescriptionfinal TestObserver<T> Assert that thisTestObserver/TestSubscriberreceived exactly oneonCompleteevent.final TestObserver<T> Assert that theTestObserver/TestSubscriberhas received aDisposable/SubscriptionviaonSubscribebut no other events.final TestObserver<T> assertError(@NonNull Predicate<Throwable> errorPredicate) Asserts that thisTestObserver/TestSubscriberreceived exactly oneonErrorevent for which the provided predicate returnstrue.final TestObserver<T> assertError(@NonNull Class<? extends Throwable> errorClass) Asserts that thisTestObserver/TestSubscriberreceived exactly oneonErrorevent which is an instance of the specifiederrorClassClass.final TestObserver<T> assertError(@NonNull Throwable error) Assert that thisTestObserver/TestSubscriberreceived exactly the specifiedonErrorevent value.final TestObserver<T> assertFailure(@NonNull Class<? extends Throwable> error, T... values) Assert that the upstream signaled the specified values in order and then failed with a specific class or subclass ofThrowable.final TestObserver<T> Assert that thisTestObserver/TestSubscriberhas not received anonErrorevent.final TestObserver<T> Assert that thisTestObserver/TestSubscriberhas not received anonCompleteevent.final TestObserver<T> Assert that thisTestObserver/TestSubscriberhas not received anyonNextevents.final TestObserver<T> assertResult(T... values) Assert that the upstream signaled the specified values in order and completed normally.final TestObserver<T> assertValue(@NonNull Predicate<T> valuePredicate) Asserts that thisTestObserver/TestSubscriberreceived exactly oneonNextvalue for which the provided predicate returnstrue.final TestObserver<T> assertValue(T value) Assert that thisTestObserver/TestSubscriberreceived exactly oneonNextvalue which is equal to the given value with respect toObjects.equals(Object, Object).final TestObserver<T> assertValueAt(int index, @NonNull Predicate<T> valuePredicate) Asserts that thisTestObserver/TestSubscriberreceived anonNextvalue at the given index for the provided predicate returnstrue.final TestObserver<T> assertValueAt(int index, T value) Asserts that thisTestObserver/TestSubscriberreceived anonNextvalue at the given index which is equal to the given value with respect tonull-safeObjects.equals(Object, Object).final TestObserver<T> assertValueCount(int count) Assert that thisTestObserver/TestSubscriberreceived the specified numberonNextevents.final TestObserver<T> assertValues(T... values) Assert that theTestObserver/TestSubscriberreceived only the specified values in the specified order.final TestObserver<T> assertValueSequence(@NonNull Iterable<? extends T> sequence) Assert that theTestObserver/TestSubscriberreceived only the specified sequence of values in the same order.final TestObserver<T> assertValuesOnly(T... values) Assert that theTestObserver/TestSubscriberreceived only the specified values in the specified order without terminating.final TestObserver<T> await()Awaits until thisTestObserver/TestSubscriberreceives anonErrororonCompleteevents.final booleanAwaits the specified amount of time or until thisTestObserver/TestSubscriberreceives anonErrororonCompleteevents, whichever happens first.final TestObserver<T> awaitCount(int atLeast) Await until theTestObserver/TestSubscriberreceives the given number of items or terminates by sleeping 10 milliseconds at a time up to 5000 milliseconds of timeout.final TestObserver<T> Awaits until the internal latch is counted down.protected final @NonNull AssertionErrorFail with the given message and add the sequence of errors as suppressed ones.Appends the class name to a non-nullvalue or returns"null".values()Returns a shared list of receivedonNextvalues or the singleonSuccessvalue.final TestObserver<T> withTag(@Nullable CharSequence tag) Set the tag displayed along with an assertion failure's other state information.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Disposable
closeModifier and TypeMethodDescriptiondefault voidclose()Dispose the resource, the operation should be idempotent.
-
Constructor Details
-
TestObserver
public TestObserver()Constructs a non-forwarding TestObserver. -
TestObserver
-
-
Method Details
-
create
Constructs a non-forwardingTestObserver.- Type Parameters:
T- the value type received- Returns:
- the new
TestObserverinstance
-
create
-
onSubscribe
Description copied from interface:ObserverProvides theObserverwith the means of cancelling (disposing) the connection (channel) with theObservablein both synchronous (from withinObserver.onNext(Object)) and asynchronous manner.- Specified by:
onSubscribein interfaceCompletableObserver- Specified by:
onSubscribein interfaceMaybeObserver<T>- Specified by:
onSubscribein interfaceObserver<T>- Specified by:
onSubscribein interfaceSingleObserver<T>- Parameters:
d- theDisposableinstance whoseDisposable.dispose()can be called anytime to cancel the connection
-
onNext
Description copied from interface:ObserverProvides theObserverwith a new item to observe.The
Observablemay call this method 0 or more times.The
Observablewill not call this method again after it calls eitherObserver.onComplete()orObserver.onError(Throwable). -
onError
Description copied from interface:ObserverNotifies theObserverthat theObservablehas experienced an error condition.If the
Observablecalls this method, it will not thereafter callObserver.onNext(T)orObserver.onComplete().- Specified by:
onErrorin interfaceCompletableObserver- Specified by:
onErrorin interfaceMaybeObserver<T>- Specified by:
onErrorin interfaceObserver<T>- Specified by:
onErrorin interfaceSingleObserver<T>- Parameters:
t- the exception encountered by the Observable
-
onComplete
public void onComplete()Description copied from interface:ObserverNotifies theObserverthat theObservablehas finished sending push-based notifications.The
Observablewill not call this method if it callsObserver.onError(Throwable).- Specified by:
onCompletein interfaceCompletableObserver- Specified by:
onCompletein interfaceMaybeObserver<T>- Specified by:
onCompletein interfaceObserver<T>
-
dispose
public final void dispose()Description copied from class:BaseTestConsumerCancel/dispose this test consumer.- Specified by:
disposein interfaceDisposable- Specified by:
disposein classBaseTestConsumer<T, TestObserver<T>>
-
isDisposed
public final boolean isDisposed()Description copied from class:BaseTestConsumerReturns true if this test consumer was cancelled/disposed.- Specified by:
isDisposedin interfaceDisposable- Specified by:
isDisposedin classBaseTestConsumer<T, TestObserver<T>>- Returns:
- true if this test consumer was cancelled/disposed.
-
hasSubscription
public final boolean hasSubscription()Returns true if thisTestObserverreceived a subscription.- Returns:
- true if this
TestObserverreceived a subscription
-
assertSubscribed
Assert that theonSubscribe(Disposable)method was called exactly once.- Specified by:
assertSubscribedin classBaseTestConsumer<T, TestObserver<T>>- Returns:
- this
-
onSuccess
Description copied from interface:MaybeObserverNotifies theMaybeObserverwith one item and that theMaybehas finished sending push-based notifications.The
Maybewill not call this method if it callsMaybeObserver.onError(Throwable).- Specified by:
onSuccessin interfaceMaybeObserver<T>- Specified by:
onSuccessin interfaceSingleObserver<T>- Parameters:
value- the item emitted by theMaybe
-