Top Description Fields Constructors Methods
io.reactivex.rxjava3.observers

public abstract Class BaseTestConsumer<T, U extends io.reactivex.rxjava3.observers.BaseTestConsumer<T, U>>

extends Object
Class Inheritance
Known Direct Subclasses
io.reactivex.rxjava3.observers.TestObserver, io.reactivex.rxjava3.subscribers.TestSubscriber
Type Parameters
<T>
the value type consumed
<U>
the subclass of this BaseTestConsumer
Imports
java.util.*, java.util.concurrent.*, io.reactivex.rxjava3.annotations.*, io.reactivex.rxjava3.exceptions.CompositeException, io.reactivex.rxjava3.functions.Predicate, io.reactivex.rxjava3.internal.functions.Functions, io.reactivex.rxjava3.internal.util.*

Base class with shared infrastructure to support TestSubscriber and TestObserver.

Field Summary

Modifier and TypeField and Description
protected boolean
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 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.

Constructor Summary

AccessConstructor and Description
public
BaseTestConsumer()

Constructs a BaseTestConsumer with CountDownLatch set to 1.

Method Summary

Modifier and TypeMethod and Description
public final U

Returns:

this
assertComplete
()

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

public final U

Returns:

this
assertEmpty
()

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

public final U

Returns:

this
assertError
(@NonNull Throwable
the error to check
error
)

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

public final U

Returns:

this
assertError
(@NonNull Class<? extends Throwable>
the error Class to expect
errorClass
)

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

public final U

Returns:

this
assertError
(@NonNull Predicate<Throwable>
the predicate that receives the error Throwable and should return true for expected errors.
errorPredicate
)

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

private U
assertError(@NonNull Predicate<Throwable> errorPredicate, boolean exact)

public final U

Returns:

this
assertFailure
(@NonNull Class<? extends Throwable>
the expected exception (parent) Class
error
,
T...
the expected values, asserted in order
values
)

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

public final U

Returns:

this
assertNoErrors
()

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

public final U

Returns:

this
assertNotComplete
()

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

public final U

Returns:

this
assertNoValues
()

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

public final U

Returns:

this
assertResult
(T...
the expected values, asserted in order
values
)

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

protected abstract U

Returns:

this
assertSubscribed
()

Assert that the onSubscribe method was called exactly once.

public final U

Returns:

this
assertValue
(T
the value to expect
value
)

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

public final U

Returns:

this
assertValue
(@NonNull Predicate<T>
the predicate that receives the onNext value and should return true for the expected value.
valuePredicate
)

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

public final U

Returns:

this
assertValueAt
(int
the position to assert on
index
,
T
the value to expect
value
)

Asserts that this TestObserver/TestSubscriber received an onNext value at the given index which is equal to the given value with respect to null-safe Objects#equals(Object, Object).

public final U

Returns:

this
assertValueAt
(int
the position to assert on
index
,
@NonNull Predicate<T>
the predicate that receives the onNext value and should return true for the expected value.
valuePredicate
)

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

public final U

Returns:

this
assertValueCount
(int
the expected number of onNext events
count
)

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

public final U

Returns:

this
assertValues
(T...
the values expected
values
)

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

public final U

Returns:

this
assertValueSequence
(@NonNull Iterable<? extends T>
the sequence of expected values in order
sequence
)

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

public final U

Returns:

this
assertValuesOnly
(T...
the values expected
values
)

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

public final U

Returns:

this
await
()

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

public final boolean

Returns:

true if the TestObserver/TestSubscriber terminated, false if timeout happened
await
(long
the waiting time
time
,
@NonNull TimeUnit
the time unit of the waiting time
unit
)

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

public final U

Returns:

this
awaitCount
(int
the number of items expected at least
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.

public final U

Returns:

this
awaitDone
(long
the waiting time
time
,
@NonNull TimeUnit
the time unit of the waiting time
unit
)

Awaits until the internal latch is counted down.

protected abstract void
dispose()

Cancel/dispose this test consumer.

protected final @NonNull AssertionError

Returns:

AssertionError the prepared AssertionError instance
fail
(@NonNull String
the message to use
message
)

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

protected abstract boolean

Returns:

true if this test consumer was cancelled/disposed.
isDisposed
()

Returns true if this test consumer was cancelled/disposed.

public static @NonNull String

Returns:

the string representation
valueAndClass
(@Nullable Object
the object
o
)

Appends the class name to a non-null value or returns "null".

public final @NonNull List<T>

Returns:

a list of received onNext values
values
()

Returns a shared list of received onNext values or the single onSuccess value.

public final U

Returns:

this
withTag
(@Nullable CharSequence
the string to display (null won't print any tag)
tag
)

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

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

checkSubscriptionOnceback to summary
protected boolean checkSubscriptionOnce
completionsback to summary
protected long completions

The number of completions.

doneback to summary
protected final CountDownLatch done

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

errorsback to summary
protected final List<Throwable> errors

The list of errors received.

lastThreadback to summary
protected Thread lastThread

The last thread seen by the observer.

tagback to summary
protected CharSequence tag

The optional tag associated with this test consumer.

Since
2.0.7
timeoutback to summary
protected boolean timeout

Indicates that one of the awaitX method has timed out.

Since
2.0.7
valuesback to summary
protected final List<T> values

The list of values received.

Constructor Detail

BaseTestConsumerback to summary
public BaseTestConsumer()

Constructs a BaseTestConsumer with CountDownLatch set to 1.

Method Detail

assertCompleteback to summary
public final U assertComplete()

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

Returns:U

this

Annotations
@SuppressWarnings:unchecked
@NonNull
assertEmptyback to summary
public final U assertEmpty()

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

Returns:U

this

Annotations
@NonNull
assertErrorback to summary
public final U assertError(@NonNull Throwable error)

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

The comparison is performed via Objects#equals(Object, Object); 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:@NonNull Throwable

the error to check

Returns:U

this

Annotations
@NonNull
See Also
assertError(Class), assertError(Predicate)
assertErrorback to summary
public final U assertError(@NonNull 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:@NonNull Class<? extends Throwable>

the error Class to expect

Returns:U

this

Annotations
@SuppressWarnings:unchecked, rawtypes
@NonNull
assertErrorback to summary
public final U assertError(@NonNull Predicate<Throwable> errorPredicate)

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

Parameters
errorPredicate:@NonNull Predicate<Throwable>

the predicate that receives the error Throwable and should return true for expected errors.

Returns:U

this

Annotations
@NonNull
assertErrorback to summary
private U assertError(@NonNull Predicate<Throwable> errorPredicate, boolean exact)
Annotations
@SuppressWarnings:unchecked
@NonNull
assertFailureback to summary
public final U assertFailure(@NonNull Class<? extends Throwable> error, T... values)

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

Parameters
error:@NonNull Class<? extends Throwable>

the expected exception (parent) Class

values:T[]

the expected values, asserted in order

Returns:U

this

Annotations
@SafeVarargs
@NonNull
assertNoErrorsback to summary
public final U assertNoErrors()

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

Returns:U

this

Annotations
@SuppressWarnings:unchecked
@NonNull
assertNotCompleteback to summary
public final U assertNotComplete()

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

Returns:U

this

Annotations
@SuppressWarnings:unchecked
@NonNull
assertNoValuesback to summary
public final U assertNoValues()

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

Returns:U

this

Annotations
@NonNull
assertResultback to summary
public final U assertResult(T... values)

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

Parameters
values:T[]

the expected values, asserted in order

Returns:U

this

Annotations
@SafeVarargs
@NonNull
See Also
assertFailure(Class, Object...)
assertSubscribedback to summary
protected abstract U assertSubscribed()

Assert that the onSubscribe method was called exactly once.

Returns:U

this

Annotations
@NonNull
assertValueback to summary
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(Object, Object).

Parameters
value:T

the value to expect

Returns:U

this

Annotations
@SuppressWarnings:unchecked
@NonNull
assertValueback to summary
public final U assertValue(@NonNull Predicate<T> valuePredicate)

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

Parameters
valuePredicate:@NonNull Predicate<T>

the predicate that receives the onNext value and should return true for the expected value.

Returns:U

this

Annotations
@SuppressWarnings:unchecked
@NonNull
assertValueAtback to summary
public final U assertValueAt(int index, T value)

Asserts that this TestObserver/TestSubscriber received an onNext value at the given index which is equal to the given value with respect to null-safe Objects#equals(Object, Object).

History: 2.1.3 - experimental

Parameters
index:int

the position to assert on

value:T

the value to expect

Returns:U

this

Annotations
@SuppressWarnings:unchecked
@NonNull
Since
2.2
assertValueAtback to summary
public final U assertValueAt(int index, @NonNull Predicate<T> valuePredicate)

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

Parameters
index:int

the position to assert on

valuePredicate:@NonNull Predicate<T>

the predicate that receives the onNext value and should return true for the expected value.

Returns:U

this

Annotations
@SuppressWarnings:unchecked
@NonNull
assertValueCountback to summary
public final U assertValueCount(int count)

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

Parameters
count:int

the expected number of onNext events

Returns:U

this

Annotations
@SuppressWarnings:unchecked
@NonNull
assertValuesback to summary
public final U assertValues(T... values)

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

Parameters
values:T[]

the values expected

Returns:U

this

Annotations
@SuppressWarnings:unchecked
@SafeVarargs
@NonNull
assertValueSequenceback to summary
public final U assertValueSequence(@NonNull Iterable<? extends T> sequence)

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

Parameters
sequence:@NonNull Iterable<? extends T>

the sequence of expected values in order

Returns:U

this

Annotations
@SuppressWarnings:unchecked
@NonNull
assertValuesOnlyback to summary
public final U assertValuesOnly(T... values)

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

History: 2.1.4 - experimental

Parameters
values:T[]

the values expected

Returns:U

this

Annotations
@SafeVarargs
@NonNull
Since
2.2
awaitback to summary
public final U await() throws InterruptedException

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

Returns:U

this

Annotations
@SuppressWarnings:unchecked
@NonNull
Exceptions
InterruptedException:
if the current thread is interrupted while waiting
awaitback to summary
public final boolean await(long time, @NonNull TimeUnit unit) throws InterruptedException

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

Parameters
time:long

the waiting time

unit:@NonNull TimeUnit

the time unit of the waiting time

Returns:boolean

true if the TestObserver/TestSubscriber terminated, false if timeout happened

Exceptions
InterruptedException:
if the current thread is interrupted while waiting
awaitCountback to summary
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:int

the number of items expected at least

Returns:U

this

Annotations
@SuppressWarnings:unchecked
@NonNull
Since
2.1
awaitDoneback to summary
public final U awaitDone(long time, @NonNull 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:long

the waiting time

unit:@NonNull TimeUnit

the time unit of the waiting time

Returns:U

this

Annotations
@SuppressWarnings:unchecked
@NonNull
Exceptions
RuntimeException:
wrapping an InterruptedException if the wait is interrupted
disposeback to summary
protected abstract void dispose()

Cancel/dispose this test consumer.

failback to summary
protected final @NonNull AssertionError fail(@NonNull 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:@NonNull String

the message to use

Returns:@NonNull AssertionError

AssertionError the prepared AssertionError instance

Annotations
@NonNull
isDisposedback to summary
protected abstract boolean isDisposed()

Returns true if this test consumer was cancelled/disposed.

Returns:boolean

true if this test consumer was cancelled/disposed.

valueAndClassback to summary
public static @NonNull String valueAndClass(@Nullable Object o)

Appends the class name to a non-null value or returns "null".

Parameters
o:@Nullable Object

the object

Returns:@NonNull String

the string representation

Annotations
@NonNull
valuesback to summary
public final @NonNull List<T> values()

Returns a shared list of received onNext values or the single onSuccess value.

Note that accessing the items via certain methods of the List interface while the upstream is still actively emitting more items may result in a ConcurrentModificationException.

The List#size() method will return the number of items already received by this TestObserver/TestSubscriber in a thread-safe manner that can be read via List#get(int)) method (index range of 0 to List.size() - 1).

A view of the returned List can be created via List#subList(int, int) by using the bounds 0 (inclusive) to List#size() (exclusive) which, when accessed in a read-only fashion, should be also thread-safe and not throw any ConcurrentModificationException.

Returns:@NonNull List<T>

a list of received onNext values

Annotations
@NonNull
withTagback to summary
public final U withTag(@Nullable CharSequence tag)

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

History: 2.0.7 - experimental

Parameters
tag:@Nullable CharSequence

the string to display (null won't print any tag)

Returns:U

this

Annotations
@SuppressWarnings:unchecked
@NonNull
Since
2.1