public class

TestObserver

extends BaseTestConsumer<T, U extends BaseTestConsumer<T, U>>
implements CompletableObserver MaybeObserver<T> Observer<T> SingleObserver<T> Disposable
java.lang.Object
   ↳ io.reactivex.observers.BaseTestConsumer<T, U extends io.reactivex.observers.BaseTestConsumer<T, U>>
     ↳ io.reactivex.observers.TestObserver<T>

Class Overview

An Observer that records events and allows making assertions about them.

You can override the onSubscribe, onNext, onError, onComplete, onSuccess and cancel methods but not the others (this is by design).

The TestObserver implements Disposable for convenience where dispose calls cancel.

Summary

[Expand]
Inherited Fields
From class io.reactivex.observers.BaseTestConsumer
Public Constructors
TestObserver()
Constructs a non-forwarding TestObserver.
TestObserver(Observer<? super T> actual)
Constructs a forwarding TestObserver.
Public Methods
final TestObserver<T> assertNotSubscribed()
Assert that the onSubscribe method hasn't been called at all.
final TestObserver<T> assertOf(Consumer<? super TestObserver<T>> check)
Run a check consumer with this TestObserver instance.
final TestObserver<T> assertSubscribed()
Assert that the onSubscribe method was called exactly once.
final void cancel()
Cancels the TestObserver (before or after the subscription happened).
static <T> TestObserver<T> create(Observer<? super T> delegate)
Constructs a forwarding TestObserver.
static <T> TestObserver<T> create()
Constructs a non-forwarding TestObserver.
final void dispose()
Dispose the resource, the operation should be idempotent.
final boolean hasSubscription()
Returns true if this TestObserver received a subscription.
final boolean isCancelled()
Returns true if this TestObserver has been cancelled.
final boolean isDisposed()
Returns true if this resource has been disposed.
void onComplete()
Called once the deferred computation completes normally.
void onError(Throwable t)
Called once if the deferred computation 'throws' an exception.
void onNext(T t)
Provides the Observer with a new item to observe.
void onSubscribe(Disposable s)
Called once by the Completable to set a Disposable on this instance which then can be used to cancel the subscription at any time.
void onSuccess(T value)
Notifies the MaybeObserver with one item and that the Maybe has finished sending push-based notifications.
[Expand]
Inherited Methods
From class io.reactivex.observers.BaseTestConsumer
From class java.lang.Object
From interface io.reactivex.CompletableObserver
From interface io.reactivex.MaybeObserver
From interface io.reactivex.Observer
From interface io.reactivex.SingleObserver
From interface io.reactivex.disposables.Disposable

Public Constructors

public TestObserver ()

Constructs a non-forwarding TestObserver.

public TestObserver (Observer<? super T> actual)

Constructs a forwarding TestObserver.

Parameters
actual the actual Observer to forward events to

Public Methods

public final TestObserver<T> assertNotSubscribed ()

Assert that the onSubscribe method hasn't been called at all.

Returns
  • this;

public final TestObserver<T> assertOf (Consumer<? super TestObserver<T>> check)

Run a check consumer with this TestObserver instance.

Parameters
check the check consumer to run
Returns
  • this

public final TestObserver<T> assertSubscribed ()

Assert that the onSubscribe method was called exactly once.

Returns
  • this;

public final void cancel ()

Cancels the TestObserver (before or after the subscription happened).

This operation is thread-safe.

This method is provided as a convenience when converting Flowable tests that cancel.

public static TestObserver<T> create (Observer<? super T> delegate)

Constructs a forwarding TestObserver.

Parameters
delegate the actual Observer to forward events to
Returns
  • the new TestObserver instance

public static TestObserver<T> create ()

Constructs a non-forwarding TestObserver.

Returns
  • the new TestObserver instance

public final void dispose ()

Dispose the resource, the operation should be idempotent.

public final boolean hasSubscription ()

Returns true if this TestObserver received a subscription.

Returns
  • true if this TestObserver received a subscription

public final boolean isCancelled ()

Returns true if this TestObserver has been cancelled.

Returns
  • true if this TestObserver has been cancelled

public final boolean isDisposed ()

Returns true if this resource has been disposed.

Returns
  • true if this resource has been disposed

public void onComplete ()

Called once the deferred computation completes normally.

public void onError (Throwable t)

Called once if the deferred computation 'throws' an exception.

Parameters
t the exception, not null.

public void onNext (T t)

Provides the Observer 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 either onComplete() or onError(Throwable).

Parameters
t the item emitted by the Observable

public void onSubscribe (Disposable s)

Called once by the Completable to set a Disposable on this instance which then can be used to cancel the subscription at any time.

Parameters
s the Disposable instance to call dispose on for cancellation, not null

public void onSuccess (T value)

Notifies the MaybeObserver with one item and that the Maybe has finished sending push-based notifications.

The Maybe will not call this method if it calls onError(Throwable).

Parameters
value the item emitted by the Maybe