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
,SingleObserver
andCompletableObserver
composite that can record events fromObservable
s,Maybe
s,Single
s andCompletable
s 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
TestObserver
implementsDisposable
for convenience where dispose calls cancel.- See Also:
TestSubscriber
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
TestObserver.EmptyObserver
An observer that ignores all events and does not report errors.
-
Field Summary
Fields Modifier and Type Field Description private Observer<? super T>
downstream
The actual observer to forward events to.private java.util.concurrent.atomic.AtomicReference<Disposable>
upstream
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
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
.void
dispose()
Cancel/dispose this test consumer.boolean
hasSubscription()
Returns true if thisTestObserver
received a subscription.boolean
isDisposed()
Returns true if this test consumer was cancelled/disposed.void
onComplete()
Notifies theObserver
that theObservable
has finished sending push-based notifications.void
onError(@NonNull java.lang.Throwable t)
Notifies theObserver
that theObservable
has experienced an error condition.void
onNext(T t)
Provides theObserver
with a new item to observe.void
onSubscribe(@NonNull Disposable d)
Provides theObserver
with the means of cancelling (disposing) the connection (channel) with theObservable
in both synchronous (from withinObserver.onNext(Object)
) and asynchronous manner.void
onSuccess(T value)
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 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
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
public void onSubscribe(@NonNull @NonNull Disposable d)
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
public void onNext(@NonNull T t)
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
public void onError(@NonNull @NonNull java.lang.Throwable t)
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
@NonNull protected final @NonNull TestObserver<T> assertSubscribed()
Assert that theonSubscribe(Disposable)
method was called exactly once.- Specified by:
assertSubscribed
in classBaseTestConsumer<T,TestObserver<T>>
- Returns:
- this
-
onSuccess
public void onSuccess(@NonNull T value)
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
-
-