public abstract class

BaseTestConsumer

extends Object
implements Disposable
java.lang.Object
   ↳ io.reactivex.observers.BaseTestConsumer<T, U extends io.reactivex.observers.BaseTestConsumer<T, U>>
Known Direct Subclasses

Class Overview

Base class with shared infrastructure to support TestSubscriber and TestObserver.

Summary

Nested Classes
enum BaseTestConsumer.TestWaitStrategy Enumeration of default wait strategies when waiting for a specific number of items in awaitCount(int, Runnable)
Fields
protected boolean checkSubscriptionOnce
protected long completions The number of completions.
protected final CountDownLatch done The latch that indicates an onError or onComplete has been called.
protected final List<Throwable> errors The list of errors received.
protected int establishedFusionMode
protected int initialFusionMode
protected Thread lastThread The last thread seen by the observer.
protected CharSequence tag The optional tag associated with this test consumer.
protected boolean timeout Indicates that one of the awaitX method has timed out.
protected final List<T> values The list of values received.
Public Constructors
BaseTestConsumer()
Public Methods
final U assertComplete()
Assert that this TestObserver/TestSubscriber received exactly one onComplete event.
final U assertEmpty()
Assert that the TestObserver/TestSubscriber has received a Disposable but no other events.
final U assertError(Throwable error)
Assert that this TestObserver/TestSubscriber received exactly the specified onError event value.
final U assertError(Class<? extends Throwable> errorClass)
Asserts that this TestObserver/TestSubscriber received exactly one onError event which is an instance of the specified errorClass class.
final U assertError(Predicate<Throwable> errorPredicate)
Asserts that this TestObserver/TestSubscriber received exactly one onError event for which the provided predicate returns true.
final U assertErrorMessage(String message)
Assert that there is a single error and it has the given message.
final U assertFailure(Predicate<Throwable> errorPredicate, T... values)
Assert that the upstream signalled the specified values in order and then failed with a Throwable for which the provided predicate returns true.
final U assertFailure(Class<? extends Throwable> error, T... values)
Assert that the upstream signalled the specified values in order and then failed with a specific class or subclass of Throwable.
final U assertFailureAndMessage(Class<? extends Throwable> error, String message, T... values)
Assert that the upstream signalled the specified values in order, then failed with a specific class or subclass of Throwable and with the given exact error message.
final U assertNever(Predicate<? super T> valuePredicate)
Asserts that this TestObserver/TestSubscriber did not receive any onNext value for which the provided predicate returns true.
final U assertNever(T value)
Assert that this TestObserver/TestSubscriber did not receive an onNext value which is equal to the given value with respect to Objects.equals.
final U assertNoErrors()
Assert that this TestObserver/TestSubscriber has not received any onError event.
final U assertNoTimeout()
Asserts that some awaitX method has not timed out.
final U assertNoValues()
Assert that this TestObserver/TestSubscriber has not received any onNext events.
final U assertNotComplete()
Assert that this TestObserver/TestSubscriber has not received any onComplete event.
abstract U assertNotSubscribed()
Assert that the onSubscribe method hasn't been called at all.
final U assertNotTerminated()
Assert that the TestObserver/TestSubscriber has not terminated (i.e., the terminal latch is still non-zero).
final U assertResult(T... values)
Assert that the upstream signalled the specified values in order and completed normally.
abstract U assertSubscribed()
Assert that the onSubscribe method was called exactly once.
final U assertTerminated()
Assert that the TestObserver/TestSubscriber terminated (i.e., the terminal latch reached zero).
final U assertTimeout()
Asserts that some awaitX method has timed out.
final U assertValue(Predicate<T> valuePredicate)
Asserts that this TestObserver/TestSubscriber received exactly one onNext value for which the provided predicate returns true.
final U assertValue(T value)
Assert that this TestObserver/TestSubscriber received exactly one onNext value which is equal to the given value with respect to Objects.equals.
final U assertValueAt(int index, Predicate<T> valuePredicate)
Asserts that this TestObserver/TestSubscriber received an onNext value at the given index for the provided predicate returns true.
final U assertValueCount(int count)
Assert that this TestObserver/TestSubscriber received the specified number onNext events.
final U assertValueSequence(Iterable<? extends T> sequence)
Assert that the TestObserver/TestSubscriber received only the specified sequence of values in the same order.
final U assertValueSet(Collection<? extends T> expected)
Assert that the TestObserver/TestSubscriber received only the specified values in any order.
final U assertValues(T... values)
Assert that the TestObserver/TestSubscriber received only the specified values in the specified order.
final U await()
Awaits until this TestObserver/TestSubscriber receives an onError or onComplete events.
final boolean await(long time, TimeUnit unit)
Awaits the specified amount of time or until this TestObserver/TestSubscriber receives an onError or onComplete events, whichever happens first.
final U awaitCount(int atLeast, Runnable waitStrategy)
Await until the TestObserver/TestSubscriber receives the given number of items or terminates by waiting according to the wait strategy and up to 5000 milliseconds of timeout.
final U awaitCount(int atLeast, Runnable waitStrategy, long timeoutMillis)
Await until the TestObserver/TestSubscriber receives the given number of items or terminates.
final U awaitCount(int atLeast)
Await until the TestObserver/TestSubscriber receives the given number of items or terminates by sleeping 10 milliseconds at a time up to 5000 milliseconds of timeout.
final U awaitDone(long time, TimeUnit unit)
Awaits until the internal latch is counted down.
final boolean awaitTerminalEvent(long duration, TimeUnit unit)
Awaits the specified amount of time or until this TestObserver/TestSubscriber receives an onError or onComplete events, whichever happens first.
final boolean awaitTerminalEvent()
Waits until the any terminal event has been received by this TestObserver/TestSubscriber or returns false if the wait has been interrupted.
final U clearTimeout()
Clears the timeout flag set by the await methods when they timed out.
final long completions()
Returns the number of times onComplete was called.
final int errorCount()
Returns the number of onError exceptions received.
final List<Throwable> errors()
Returns a shared list of received onError exceptions.
final List<List<Object>> getEvents()
Returns a list of 3 other lists: the first inner list contains the plain values received; the second list contains the potential errors and the final list contains the potential completions as Notifications.
final boolean isTerminated()
Returns true if TestObserver/TestSubscriber received any onError or onComplete events.
final boolean isTimeout()
final Thread lastThread()
Returns the last thread which called the onXXX methods of this TestObserver/TestSubscriber.
static String valueAndClass(Object o)
Appends the class name to a non-null value.
final int valueCount()
Returns the number of onNext values received.
final List<T> values()
Returns a shared list of received onNext values.
final U withTag(CharSequence tag)
Set the tag displayed along with an assertion failure's other state information.
Protected Methods
final AssertionError fail(String message)
Fail with the given message and add the sequence of errors as suppressed ones.
[Expand]
Inherited Methods
From class java.lang.Object
From interface io.reactivex.disposables.Disposable

Fields

protected boolean checkSubscriptionOnce

protected long completions

The number of completions.

protected final CountDownLatch done

The latch that indicates an onError or onComplete has been called.

protected final List<Throwable> errors

The list of errors received.

protected int establishedFusionMode

protected int initialFusionMode

protected Thread lastThread

The last thread seen by the observer.

protected CharSequence tag

The optional tag associated with this test consumer.

protected boolean timeout

Indicates that one of the awaitX method has timed out.

protected final List<T> values

The list of values received.

Public Constructors

public BaseTestConsumer ()

Public Methods

public final U assertComplete ()

Assert that this TestObserver/TestSubscriber received exactly one onComplete event.

Returns
  • this;

public final U assertEmpty ()

Assert that the TestObserver/TestSubscriber has received a Disposable but no other events.

Returns
  • this

public final U assertError (Throwable error)

Assert that this TestObserver/TestSubscriber received exactly the specified onError event value.

The comparison is performed via Objects.equals(); since most exceptions don't implement equals(), this assertion may fail. Use the assertError(Class) overload to test against the class of an error instead of an instance of an error or assertError(Predicate) to test with different condition.

Parameters
error the error to check
Returns
  • this;

public final U assertError (Class<? extends Throwable> errorClass)

Asserts that this TestObserver/TestSubscriber received exactly one onError event which is an instance of the specified errorClass class.

Parameters
errorClass the error class to expect
Returns
  • this;

public final U assertError (Predicate<Throwable> errorPredicate)

Asserts that this TestObserver/TestSubscriber received exactly one onError event for which the provided predicate returns true.

Parameters
errorPredicate the predicate that receives the error Throwable and should return true for expected errors.
Returns
  • this

public final U assertErrorMessage (String message)

Assert that there is a single error and it has the given message.

Parameters
message the message expected
Returns
  • this

public final U assertFailure (Predicate<Throwable> errorPredicate, T... values)

Assert that the upstream signalled the specified values in order and then failed with a Throwable for which the provided predicate returns true.

Parameters
errorPredicate the predicate that receives the error Throwable and should return true for expected errors.
values the expected values, asserted in order
Returns
  • this

public final U assertFailure (Class<? extends Throwable> error, T... values)

Assert that the upstream signalled the specified values in order and then failed with a specific class or subclass of Throwable.

Parameters
error the expected exception (parent) class
values the expected values, asserted in order
Returns
  • this

public final U assertFailureAndMessage (Class<? extends Throwable> error, String message, T... values)

Assert that the upstream signalled the specified values in order, then failed with a specific class or subclass of Throwable and with the given exact error message.

Parameters
error the expected exception (parent) class
message the expected failure message
values the expected values, asserted in order
Returns
  • this

public final U assertNever (Predicate<? super T> valuePredicate)

Asserts that this TestObserver/TestSubscriber did not receive any onNext value for which the provided predicate returns true.

History: 2.0.5 - experimental

Parameters
valuePredicate the predicate that receives the onNext value and should return true for the expected value.
Returns
  • this

public final U assertNever (T value)

Assert that this TestObserver/TestSubscriber did not receive an onNext value which is equal to the given value with respect to Objects.equals.

History: 2.0.5 - experimental

Parameters
value the value to expect not being received
Returns
  • this

public final U assertNoErrors ()

Assert that this TestObserver/TestSubscriber has not received any onError event.

Returns
  • this;

public final U assertNoTimeout ()

Asserts that some awaitX method has not timed out.

History: 2.0.7 - experimental

Returns
  • this

public final U assertNoValues ()

Assert that this TestObserver/TestSubscriber has not received any onNext events.

Returns
  • this;

public final U assertNotComplete ()

Assert that this TestObserver/TestSubscriber has not received any onComplete event.

Returns
  • this;

public abstract U assertNotSubscribed ()

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

Returns
  • this;

public final U assertNotTerminated ()

Assert that the TestObserver/TestSubscriber has not terminated (i.e., the terminal latch is still non-zero).

Returns
  • this;

public final U assertResult (T... values)

Assert that the upstream signalled the specified values in order and completed normally.

Parameters
values the expected values, asserted in order
Returns
  • this
See Also
  • #assertFailure(Class, Object...)
  • #assertFailure(Predicate, Object...)
  • #assertFailureAndMessage(Class, String, Object...)

public abstract U assertSubscribed ()

Assert that the onSubscribe method was called exactly once.

Returns
  • this;

public final U assertTerminated ()

Assert that the TestObserver/TestSubscriber terminated (i.e., the terminal latch reached zero).

Returns
  • this;

public final U assertTimeout ()

Asserts that some awaitX method has timed out.

History: 2.0.7 - experimental

Returns
  • this

public final U assertValue (Predicate<T> valuePredicate)

Asserts that this TestObserver/TestSubscriber received exactly one onNext value for which the provided predicate returns true.

Parameters
valuePredicate the predicate that receives the onNext value and should return true for the expected value.
Returns
  • this

public final U assertValue (T value)

Assert that this TestObserver/TestSubscriber received exactly one onNext value which is equal to the given value with respect to Objects.equals.

Parameters
value the value to expect
Returns
  • this;

public final U assertValueAt (int index, Predicate<T> valuePredicate)

Asserts that this TestObserver/TestSubscriber received an onNext value at the given index for the provided predicate returns true.

Parameters
index the position to assert on
valuePredicate the predicate that receives the onNext value and should return true for the expected value.
Returns
  • this

public final U assertValueCount (int count)

Assert that this TestObserver/TestSubscriber received the specified number onNext events.

Parameters
count the expected number of onNext events
Returns
  • this;

public final U assertValueSequence (Iterable<? extends T> sequence)

Assert that the TestObserver/TestSubscriber received only the specified sequence of values in the same order.

Parameters
sequence the sequence of expected values in order
Returns
  • this;

public final U assertValueSet (Collection<? extends T> expected)

Assert that the TestObserver/TestSubscriber received only the specified values in any order.

This helps asserting when the order of the values is not guaranteed, i.e., when merging asynchronous streams.

Parameters
expected the collection of values expected in any order
Returns
  • this;

public final U assertValues (T... values)

Assert that the TestObserver/TestSubscriber received only the specified values in the specified order.

Parameters
values the values expected
Returns
  • this;

public final U await ()

Awaits until this TestObserver/TestSubscriber receives an onError or onComplete events.

Returns
  • this
Throws
InterruptedException if the current thread is interrupted while waiting

public final boolean await (long time, TimeUnit unit)

Awaits the specified amount of time or until this TestObserver/TestSubscriber receives an onError or onComplete events, whichever happens first.

Parameters
time the waiting time
unit the time unit of the waiting time
Returns
  • true if the TestObserver/TestSubscriber terminated, false if timeout happened
Throws
InterruptedException if the current thread is interrupted while waiting

public final U awaitCount (int atLeast, Runnable waitStrategy)

Await until the TestObserver/TestSubscriber receives the given number of items or terminates by waiting according to the wait strategy and up to 5000 milliseconds of timeout.

History: 2.0.7 - experimental

Parameters
atLeast the number of items expected at least
waitStrategy a Runnable called when the current received count hasn't reached the expected value and there was no terminal event either, see BaseTestConsumer.TestWaitStrategy for examples
Returns
  • this

public final U awaitCount (int atLeast, Runnable waitStrategy, long timeoutMillis)

Await until the TestObserver/TestSubscriber receives the given number of items or terminates.

History: 2.0.7 - experimental

Parameters
atLeast the number of items expected at least
waitStrategy a Runnable called when the current received count hasn't reached the expected value and there was no terminal event either, see BaseTestConsumer.TestWaitStrategy for examples
timeoutMillis if positive, the await ends if the specified amount of time has passed no matter how many items were received
Returns
  • this

public final U awaitCount (int atLeast)

Await until the TestObserver/TestSubscriber receives the given number of items or terminates by sleeping 10 milliseconds at a time up to 5000 milliseconds of timeout.

History: 2.0.7 - experimental

Parameters
atLeast the number of items expected at least
Returns
  • this

public final U awaitDone (long time, TimeUnit unit)

Awaits until the internal latch is counted down.

If the wait times out or gets interrupted, the TestObserver/TestSubscriber is cancelled.

Parameters
time the waiting time
unit the time unit of the waiting time
Returns
  • this
Throws
RuntimeException wrapping an InterruptedException if the wait is interrupted

public final boolean awaitTerminalEvent (long duration, TimeUnit unit)

Awaits the specified amount of time or until this TestObserver/TestSubscriber receives an onError or onComplete events, whichever happens first.

Parameters
duration the waiting time
unit the time unit of the waiting time
Returns
  • true if the TestObserver/TestSubscriber terminated, false if timeout or interrupt happened

public final boolean awaitTerminalEvent ()

Waits until the any terminal event has been received by this TestObserver/TestSubscriber or returns false if the wait has been interrupted.

Returns
  • true if the TestObserver/TestSubscriber terminated, false if the wait has been interrupted

public final U clearTimeout ()

Clears the timeout flag set by the await methods when they timed out.

History: 2.0.7 - experimental

Returns
  • this
See Also

public final long completions ()

Returns the number of times onComplete was called.

Returns
  • the number of times onComplete was called

public final int errorCount ()

Returns the number of onError exceptions received.

Returns
  • the number of onError exceptions received

public final List<Throwable> errors ()

Returns a shared list of received onError exceptions.

Returns
  • a list of received events onError exceptions

public final List<List<Object>> getEvents ()

Returns a list of 3 other lists: the first inner list contains the plain values received; the second list contains the potential errors and the final list contains the potential completions as Notifications.

Returns
  • a list of (values, errors, completion-notifications)

public final boolean isTerminated ()

Returns true if TestObserver/TestSubscriber received any onError or onComplete events.

Returns
  • true if TestObserver/TestSubscriber received any onError or onComplete events

public final boolean isTimeout ()

Returns
  • true if one of the timeout-based await methods has timed out.

    History: 2.0.7 - experimental

public final Thread lastThread ()

Returns the last thread which called the onXXX methods of this TestObserver/TestSubscriber.

Returns
  • the last thread which called the onXXX methods

public static String valueAndClass (Object o)

Appends the class name to a non-null value.

Parameters
o the object
Returns
  • the string representation

public final int valueCount ()

Returns the number of onNext values received.

Returns
  • the number of onNext values received

public final List<T> values ()

Returns a shared list of received onNext values.

Returns
  • a list of received onNext values

public final U withTag (CharSequence tag)

Set the tag displayed along with an assertion failure's other state information.

History: 2.0.7 - experimental

Parameters
tag the string to display (null won't print any tag)
Returns
  • this

Protected Methods

protected final AssertionError fail (String message)

Fail with the given message and add the sequence of errors as suppressed ones.

Note this is deliberately the only fail method. Most of the times an assertion would fail but it is possible it was due to an exception somewhere. This construct will capture those potential errors and report it along with the original failure.

Parameters
message the message to use
Returns
  • AssertionError the prepared AssertionError instance