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
An
Observer
, MaybeObserver
, SingleObserver
and
CompletableObserver
composite that can record events from
Observable
s, Maybe
s, Single
s and Completable
s
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static enum
An observer that ignores all events and does not report errors. -
Field Summary
FieldsModifier and TypeFieldDescriptionThe actual observer to forward events to.private final AtomicReference
<Disposable> Holds the current subscription if any.Fields inherited from class io.reactivex.rxjava3.observers.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 void
dispose()
Cancel/dispose this test consumer.final boolean
Returns true if thisTestObserver
received a subscription.final boolean
Returns true if this test consumer was cancelled/disposed.void
Notifies theObserver
that theObservable
has finished sending push-based notifications.void
Notifies theObserver
that theObservable
has experienced an error condition.void
Provides theObserver
with a new item to observe.void
Provides theObserver
with the means of cancelling (disposing) the connection (channel) with theObservable
in both synchronous (from withinObserver.onNext(Object)
) and asynchronous manner.void
Notifies theMaybeObserver
with one item and that theMaybe
has 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 Details
-
downstream
The actual observer to forward events to. -
upstream
Holds the current subscription if any.
-
-
Constructor Details
-
TestObserver
public TestObserver()Constructs a non-forwarding TestObserver. -
TestObserver
Constructs a forwardingTestObserver
.- Parameters:
downstream
- the actualObserver
to forward events to
-
-
Method Details
-
create
Constructs a non-forwardingTestObserver
.- Type Parameters:
T
- the value type received- Returns:
- the new
TestObserver
instance
-
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 actualObserver
to forward events to- Returns:
- the new
TestObserver
instance
-
onSubscribe
Description copied from interface:Observer
Provides theObserver
with the means of cancelling (disposing) the connection (channel) with theObservable
in both synchronous (from withinObserver.onNext(Object)
) and asynchronous manner.- Specified by:
onSubscribe
in interfaceCompletableObserver
- Specified by:
onSubscribe
in interfaceMaybeObserver<T>
- Specified by:
onSubscribe
in interfaceObserver<T>
- Specified by:
onSubscribe
in interfaceSingleObserver<T>
- Parameters:
d
- theDisposable
instance whoseDisposable.dispose()
can be called anytime to cancel the connection
-
onNext
Description copied from interface:Observer
Provides theObserver
with a new item to observe.The
Observable
may call this method 0 or more times.The
Observable
will not call this method again after it calls eitherObserver.onComplete()
orObserver.onError(java.lang.Throwable)
. -
onError
Description copied from interface:Observer
Notifies theObserver
that theObservable
has experienced an error condition.If the
Observable
calls this method, it will not thereafter callObserver.onNext(T)
orObserver.onComplete()
.- Specified by:
onError
in interfaceCompletableObserver
- Specified by:
onError
in interfaceMaybeObserver<T>
- Specified by:
onError
in interfaceObserver<T>
- Specified by:
onError
in interfaceSingleObserver<T>
- Parameters:
t
- the exception encountered by the Observable
-
onComplete
public void onComplete()Description copied from interface:Observer
Notifies theObserver
that theObservable
has finished sending push-based notifications.The
Observable
will not call this method if it callsObserver.onError(java.lang.Throwable)
.- Specified by:
onComplete
in interfaceCompletableObserver
- Specified by:
onComplete
in interfaceMaybeObserver<T>
- Specified by:
onComplete
in interfaceObserver<T>
-
dispose
public final void dispose()Description copied from class:BaseTestConsumer
Cancel/dispose this test consumer.- Specified by:
dispose
in interfaceDisposable
- Specified by:
dispose
in classBaseTestConsumer<T,
TestObserver<T>>
-
isDisposed
public final boolean isDisposed()Description copied from class:BaseTestConsumer
Returns true if this test consumer was cancelled/disposed.- Specified by:
isDisposed
in interfaceDisposable
- Specified by:
isDisposed
in classBaseTestConsumer<T,
TestObserver<T>> - Returns:
- true if this test consumer was cancelled/disposed.
-
hasSubscription
public final boolean hasSubscription()Returns true if thisTestObserver
received a subscription.- Returns:
- true if this
TestObserver
received a subscription
-
assertSubscribed
Assert that theonSubscribe(Disposable)
method was called exactly once.- Specified by:
assertSubscribed
in classBaseTestConsumer<T,
TestObserver<T>> - Returns:
- this
-
onSuccess
Description copied from interface:MaybeObserver
Notifies theMaybeObserver
with one item and that theMaybe
has finished sending push-based notifications.The
Maybe
will not call this method if it callsMaybeObserver.onError(java.lang.Throwable)
.- Specified by:
onSuccess
in interfaceMaybeObserver<T>
- Specified by:
onSuccess
in interfaceSingleObserver<T>
- Parameters:
value
- the item emitted by theMaybe
-