public class

TestSubscriber

extends BaseTestConsumer<T, U extends BaseTestConsumer<T, U>>
implements FlowableSubscriber<T> Disposable Subscription
java.lang.Object
   ↳ io.reactivex.observers.BaseTestConsumer<T, U extends io.reactivex.observers.BaseTestConsumer<T, U>>
     ↳ io.reactivex.subscribers.TestSubscriber<T>

Class Overview

A subscriber that records events and allows making assertions about them.

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

The TestSubscriber implements Disposable for convenience where dispose calls cancel.

When calling the default request method, you are requesting on behalf of the wrapped actual subscriber.

Summary

[Expand]
Inherited Fields
From class io.reactivex.observers.BaseTestConsumer
Public Constructors
TestSubscriber()
Constructs a non-forwarding TestSubscriber with an initial request value of Long.MAX_VALUE.
TestSubscriber(long initialRequest)
Constructs a non-forwarding TestSubscriber with the specified initial request value.
TestSubscriber(Subscriber<? super T> actual)
Constructs a forwarding TestSubscriber but leaves the requesting to the wrapped subscriber.
TestSubscriber(Subscriber<? super T> actual, long initialRequest)
Constructs a forwarding TestSubscriber with the specified initial request value.
Public Methods
final TestSubscriber<T> assertNotSubscribed()
Assert that the onSubscribe method hasn't been called at all.
final TestSubscriber<T> assertOf(Consumer<? super TestSubscriber<T>> check)
Run a check consumer with this TestSubscriber instance.
final TestSubscriber<T> assertSubscribed()
Assert that the onSubscribe method was called exactly once.
final void cancel()
static <T> TestSubscriber<T> create(long initialRequested)
Creates a TestSubscriber with the given initial request.
static <T> TestSubscriber<T> create()
Creates a TestSubscriber with Long.MAX_VALUE initial request.
static <T> TestSubscriber<T> create(Subscriber<? super T> delegate)
Constructs a forwarding TestSubscriber.
final void dispose()
Dispose the resource, the operation should be idempotent.
final boolean hasSubscription()
Returns true if this TestSubscriber received a subscription.
final boolean isCancelled()
Returns true if this TestSubscriber has been cancelled.
final boolean isDisposed()
Returns true if this resource has been disposed.
void onComplete()
void onError(Throwable t)
void onNext(T t)
void onSubscribe(Subscription s)
Implementors of this method should make sure everything that needs to be visible in onNext(Object) is established before calling request(long).
final void request(long n)
final TestSubscriber<T> requestMore(long n)
Calls request(long) and returns this.
Protected Methods
void onStart()
Called after the onSubscribe is called and handled.
[Expand]
Inherited Methods
From class io.reactivex.observers.BaseTestConsumer
From class java.lang.Object
From interface io.reactivex.FlowableSubscriber
From interface io.reactivex.disposables.Disposable
From interface org.reactivestreams.Subscriber
From interface org.reactivestreams.Subscription

Public Constructors

public TestSubscriber ()

Constructs a non-forwarding TestSubscriber with an initial request value of Long.MAX_VALUE.

public TestSubscriber (long initialRequest)

Constructs a non-forwarding TestSubscriber with the specified initial request value.

The TestSubscriber doesn't validate the initialRequest value so one can test sources with invalid values as well.

Parameters
initialRequest the initial request value

public TestSubscriber (Subscriber<? super T> actual)

Constructs a forwarding TestSubscriber but leaves the requesting to the wrapped subscriber.

Parameters
actual the actual Subscriber to forward events to

public TestSubscriber (Subscriber<? super T> actual, long initialRequest)

Constructs a forwarding TestSubscriber with the specified initial request value.

The TestSubscriber doesn't validate the initialRequest value so one can test sources with invalid values as well.

Parameters
actual the actual Subscriber to forward events to
initialRequest the initial request value

Public Methods

public final TestSubscriber<T> assertNotSubscribed ()

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

Returns
  • this

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

Run a check consumer with this TestSubscriber instance.

Parameters
check the check consumer to run
Returns
  • this

public final TestSubscriber<T> assertSubscribed ()

Assert that the onSubscribe method was called exactly once.

Returns
  • this

public final void cancel ()

public static TestSubscriber<T> create (long initialRequested)

Creates a TestSubscriber with the given initial request.

Parameters
initialRequested the initial requested amount
Returns
  • the new TestSubscriber instance.

public static TestSubscriber<T> create ()

Creates a TestSubscriber with Long.MAX_VALUE initial request.

Returns
  • the new TestSubscriber instance.

public static TestSubscriber<T> create (Subscriber<? super T> delegate)

Constructs a forwarding TestSubscriber.

Parameters
delegate the actual Subscriber to forward events to
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 TestSubscriber received a subscription.

Returns
  • true if this TestSubscriber received a subscription

public final boolean isCancelled ()

Returns true if this TestSubscriber has been cancelled.

Returns
  • true if this TestSubscriber 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 ()

public void onError (Throwable t)

public void onNext (T t)

public void onSubscribe (Subscription s)

Implementors of this method should make sure everything that needs to be visible in onNext(Object) is established before calling request(long). In practice this means no initialization should happen after the request() call and additional behavior is thread safe in respect to onNext.

public final void request (long n)

public final TestSubscriber<T> requestMore (long n)

Calls request(long) and returns this.

History: 2.0.1 - experimental

Parameters
n the request amount
Returns
  • this

Protected Methods

protected void onStart ()

Called after the onSubscribe is called and handled.