Package io.reactivex.rxjava3.observers
Class TestObserver<T>
- java.lang.Object
-
- io.reactivex.rxjava3.observers.BaseTestConsumer<T,TestObserver<T>>
-
- io.reactivex.rxjava3.observers.TestObserver<T>
-
- Type Parameters:
T- the value type
- All Implemented Interfaces:
CompletableObserver,MaybeObserver<T>,Observer<T>,SingleObserver<T>,Disposable
public class TestObserver<T> extends BaseTestConsumer<T,TestObserver<T>> implements Observer<T>, Disposable, MaybeObserver<T>, SingleObserver<T>, CompletableObserver
AnObserver,MaybeObserver,SingleObserverandCompletableObservercomposite that can record events fromObservables,Maybes,Singles andCompletables and allows making assertions about them.You can override the
onSubscribe(Disposable),onNext(Object),onError(Throwable),onComplete()andonSuccess(Object)methods but not the others (this is by design).The
TestObserverimplementsDisposablefor convenience where dispose calls cancel.- See Also:
TestSubscriber
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classTestObserver.EmptyObserverAn observer that ignores all events and does not report errors.
-
Field Summary
Fields Modifier and Type Field Description private Observer<? super T>downstreamThe actual observer to forward events to.private java.util.concurrent.atomic.AtomicReference<Disposable>upstreamHolds the current subscription if any.-
Fields inherited from class io.reactivex.rxjava3.observers.BaseTestConsumer
checkSubscriptionOnce, completions, done, errors, lastThread, tag, timeout, values
-
-
Constructor Summary
Constructors Constructor Description TestObserver()Constructs a non-forwarding TestObserver.TestObserver(@NonNull Observer<? super T> downstream)Constructs a forwardingTestObserver.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected @NonNull TestObserver<T>assertSubscribed()Assert that theonSubscribe(Disposable)method was called exactly once.static <T> @NonNull TestObserver<T>create()Constructs a non-forwardingTestObserver.static <T> @NonNull TestObserver<T>create(@NonNull Observer<? super T> delegate)Constructs a forwardingTestObserver.voiddispose()Cancel/dispose this test consumer.booleanhasSubscription()Returns true if thisTestObserverreceived a subscription.booleanisDisposed()Returns true if this test consumer was cancelled/disposed.voidonComplete()Notifies theObserverthat theObservablehas finished sending push-based notifications.voidonError(@NonNull java.lang.Throwable t)Notifies theObserverthat theObservablehas experienced an error condition.voidonNext(T t)Provides theObserverwith a new item to observe.voidonSubscribe(@NonNull Disposable d)Provides theObserverwith the means of cancelling (disposing) the connection (channel) with theObservablein both synchronous (from withinObserver.onNext(Object)) and asynchronous manner.voidonSuccess(T value)Notifies theMaybeObserverwith one item and that theMaybehas finished sending push-based notifications.-
Methods inherited from class io.reactivex.rxjava3.observers.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, withTag
-
-
-
-
Field Detail
-
upstream
private final java.util.concurrent.atomic.AtomicReference<Disposable> upstream
Holds the current subscription if any.
-
-
Method Detail
-
create
@NonNull public static <T> @NonNull TestObserver<T> create()
Constructs a non-forwardingTestObserver.- Type Parameters:
T- the value type received- Returns:
- the new
TestObserverinstance
-
create
@NonNull public static <T> @NonNull TestObserver<T> create(@NonNull @NonNull Observer<? super T> delegate)
Constructs a forwardingTestObserver.- Type Parameters:
T- the value type received- Parameters:
delegate- the actualObserverto forward events to- Returns:
- the new
TestObserverinstance
-
onSubscribe
public void onSubscribe(@NonNull @NonNull Disposable d)
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
public void onNext(@NonNull T t)
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(java.lang.Throwable).
-
onError
public void onError(@NonNull @NonNull java.lang.Throwable t)
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(java.lang.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
@NonNull protected final @NonNull TestObserver<T> assertSubscribed()
Assert that theonSubscribe(Disposable)method was called exactly once.- Specified by:
assertSubscribedin classBaseTestConsumer<T,TestObserver<T>>- Returns:
- this
-
onSuccess
public void onSuccess(@NonNull T value)
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(java.lang.Throwable).- Specified by:
onSuccessin interfaceMaybeObserver<T>- Specified by:
onSuccessin interfaceSingleObserver<T>- Parameters:
value- the item emitted by theMaybe
-
-