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
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, values -
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, withTag
-
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
-