Class BaseTestConsumer<T,U extends BaseTestConsumer<T,U>>
- Type Parameters:
T
- the value type consumedU
- the subclass of thisBaseTestConsumer
- Direct Known Subclasses:
TestObserver
,TestSubscriber
TestSubscriber
and TestObserver
.-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected boolean
protected long
The number of completions.protected final CountDownLatch
The latch that indicates an onError or onComplete has been called.The list of errors received.protected Thread
The last thread seen by the observer.protected CharSequence
The optional tag associated with this test consumer.protected boolean
Indicates that one of theawaitX
method has timed out.The list of values received. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs aBaseTestConsumer
withCountDownLatch
set to 1. -
Method Summary
Modifier and TypeMethodDescriptionfinal U
Assert that thisTestObserver
/TestSubscriber
received exactly oneonComplete
event.final U
Assert that theTestObserver
/TestSubscriber
has received aDisposable
/Subscription
viaonSubscribe
but no other events.final U
assertError
(@NonNull Predicate<Throwable> errorPredicate) Asserts that thisTestObserver
/TestSubscriber
received exactly oneonError
event for which the provided predicate returnstrue
.private U
assertError
(@NonNull Predicate<Throwable> errorPredicate, boolean exact) final U
assertError
(@NonNull Class<? extends Throwable> errorClass) Asserts that thisTestObserver
/TestSubscriber
received exactly oneonError
event which is an instance of the specifiederrorClass
Class
.final U
assertError
(@NonNull Throwable error) Assert that thisTestObserver
/TestSubscriber
received exactly the specifiedonError
event value.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 ofThrowable
.final U
Assert that thisTestObserver
/TestSubscriber
has not received anonError
event.final U
Assert that thisTestObserver
/TestSubscriber
has not received anonComplete
event.final U
Assert that thisTestObserver
/TestSubscriber
has not received anyonNext
events.final U
assertResult
(T... values) Assert that the upstream signaled the specified values in order and completed normally.protected abstract U
Assert that theonSubscribe
method was called exactly once.final U
assertValue
(@NonNull Predicate<T> valuePredicate) Asserts that thisTestObserver
/TestSubscriber
received exactly oneonNext
value for which the provided predicate returnstrue
.final U
assertValue
(T value) Assert that thisTestObserver
/TestSubscriber
received exactly oneonNext
value which is equal to the given value with respect toObjects.equals(Object, Object)
.final U
assertValueAt
(int index, @NonNull Predicate<T> valuePredicate) Asserts that thisTestObserver
/TestSubscriber
received anonNext
value at the given index for the provided predicate returnstrue
.final U
assertValueAt
(int index, T value) Asserts that thisTestObserver
/TestSubscriber
received anonNext
value at the given index which is equal to the given value with respect tonull
-safeObjects.equals(Object, Object)
.final U
assertValueCount
(int count) Assert that thisTestObserver
/TestSubscriber
received the specified numberonNext
events.final U
assertValues
(T... values) Assert that theTestObserver
/TestSubscriber
received only the specified values in the specified order.final U
assertValueSequence
(@NonNull Iterable<? extends T> sequence) Assert that theTestObserver
/TestSubscriber
received only the specified sequence of values in the same order.final U
assertValuesOnly
(T... values) Assert that theTestObserver
/TestSubscriber
received only the specified values in the specified order without terminating.final U
await()
Awaits until thisTestObserver
/TestSubscriber
receives anonError
oronComplete
events.final boolean
Awaits the specified amount of time or until thisTestObserver
/TestSubscriber
receives anonError
oronComplete
events, whichever happens first.final U
awaitCount
(int atLeast) Await until theTestObserver
/TestSubscriber
receives the given number of items or terminates by sleeping 10 milliseconds at a time up to 5000 milliseconds of timeout.final U
Awaits until the internal latch is counted down.protected abstract void
dispose()
Cancel/dispose this test consumer.protected final @NonNull AssertionError
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.Appends the class name to a non-null
value or returns"null"
.values()
Returns a shared list of receivedonNext
values or the singleonSuccess
value.final U
withTag
(@Nullable CharSequence tag) Set the tag displayed along with an assertion failure's other state information.
-
Field Details
-
done
The latch that indicates an onError or onComplete has been called. -
values
The list of values received. -
errors
The list of errors received. -
completions
protected long completionsThe number of completions. -
lastThread
The last thread seen by the observer. -
checkSubscriptionOnce
protected boolean checkSubscriptionOnce -
tag
The optional tag associated with this test consumer.- Since:
- 2.0.7
-
timeout
protected boolean timeoutIndicates that one of theawaitX
method has timed out.- Since:
- 2.0.7
-
-
Constructor Details
-
BaseTestConsumer
public BaseTestConsumer()Constructs aBaseTestConsumer
withCountDownLatch
set to 1.
-
-
Method Details
-
values
Returns a shared list of receivedonNext
values or the singleonSuccess
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 aConcurrentModificationException
.The
List.size()
method will return the number of items already received by thisTestObserver
/TestSubscriber
in a thread-safe manner that can be read viaList.get(int)
) method (index range of 0 toList.size() - 1
).A view of the returned List can be created via
List.subList(int, int)
by using the bounds 0 (inclusive) toList.size()
(exclusive) which, when accessed in a read-only fashion, should be also thread-safe and not throw anyConcurrentModificationException
.- Returns:
- a list of received onNext values
-
fail
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
-
await
Awaits until thisTestObserver
/TestSubscriber
receives anonError
oronComplete
events.- Returns:
- this
- Throws:
InterruptedException
- if the current thread is interrupted while waiting
-
await
Awaits the specified amount of time or until thisTestObserver
/TestSubscriber
receives anonError
oronComplete
events, whichever happens first.- Parameters:
time
- the waiting timeunit
- 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
-
assertComplete
Assert that thisTestObserver
/TestSubscriber
received exactly oneonComplete
event.- Returns:
- this
-
assertNotComplete
Assert that thisTestObserver
/TestSubscriber
has not received anonComplete
event.- Returns:
- this
-
assertNoErrors
Assert that thisTestObserver
/TestSubscriber
has not received anonError
event.- Returns:
- this
-
assertError
Assert that thisTestObserver
/TestSubscriber
received exactly the specifiedonError
event value.The comparison is performed via
Objects.equals(Object, Object)
; since most exceptions don't implement equals(), this assertion may fail. Use theassertError(Class)
overload to test against the class of an error instead of an instance of an error orassertError(Predicate)
to test with different condition.- Parameters:
error
- the error to check- Returns:
- this
- See Also:
-
assertError
Asserts that thisTestObserver
/TestSubscriber
received exactly oneonError
event which is an instance of the specifiederrorClass
Class
.- Parameters:
errorClass
- the errorClass
to expect- Returns:
- this
-
assertError
Asserts that thisTestObserver
/TestSubscriber
received exactly oneonError
event for which the provided predicate returnstrue
.- Parameters:
errorPredicate
- the predicate that receives the errorThrowable
and should returntrue
for expected errors.- Returns:
- this
-
assertError
-
assertValue
Assert that thisTestObserver
/TestSubscriber
received exactly oneonNext
value which is equal to the given value with respect toObjects.equals(Object, Object)
.- Parameters:
value
- the value to expect- Returns:
- this
-
assertValue
Asserts that thisTestObserver
/TestSubscriber
received exactly oneonNext
value for which the provided predicate returnstrue
.- Parameters:
valuePredicate
- the predicate that receives theonNext
value and should returntrue
for the expected value.- Returns:
- this
-
assertValueAt
Asserts that thisTestObserver
/TestSubscriber
received anonNext
value at the given index which is equal to the given value with respect tonull
-safeObjects.equals(Object, Object)
.History: 2.1.3 - experimental
- Parameters:
index
- the position to assert onvalue
- the value to expect- Returns:
- this
- Since:
- 2.2
-
assertValueAt
Asserts that thisTestObserver
/TestSubscriber
received anonNext
value at the given index for the provided predicate returnstrue
.- Parameters:
index
- the position to assert onvaluePredicate
- the predicate that receives theonNext
value and should returntrue
for the expected value.- Returns:
- this
-
valueAndClass
Appends the class name to a non-null
value or returns"null"
.- Parameters:
o
- the object- Returns:
- the string representation
-
assertValueCount
Assert that thisTestObserver
/TestSubscriber
received the specified numberonNext
events.- Parameters:
count
- the expected number ofonNext
events- Returns:
- this
-
assertNoValues
Assert that thisTestObserver
/TestSubscriber
has not received anyonNext
events.- Returns:
- this
-
assertValues
Assert that theTestObserver
/TestSubscriber
received only the specified values in the specified order.- Parameters:
values
- the values expected- Returns:
- this
-
assertValuesOnly
Assert that theTestObserver
/TestSubscriber
received only the specified values in the specified order without terminating.History: 2.1.4 - experimental
- Parameters:
values
- the values expected- Returns:
- this
- Since:
- 2.2
-
assertValueSequence
Assert that theTestObserver
/TestSubscriber
received only the specified sequence of values in the same order.- Parameters:
sequence
- the sequence of expected values in order- Returns:
- this
-
assertSubscribed
Assert that theonSubscribe
method was called exactly once.- Returns:
- this
-
assertResult
Assert that the upstream signaled the specified values in order and completed normally.- Parameters:
values
- the expected values, asserted in order- Returns:
- this
- See Also:
-
assertFailure
@SafeVarargs @NonNull public final U assertFailure(@NonNull @NonNull Class<? extends Throwable> error, @NonNull T... values) Assert that the upstream signaled the specified values in order and then failed with a specific class or subclass ofThrowable
.- Parameters:
error
- the expected exception (parent)Class
values
- the expected values, asserted in order- Returns:
- this
-
awaitDone
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 timeunit
- the time unit of the waiting time- Returns:
- this
- Throws:
RuntimeException
- wrapping anInterruptedException
if the wait is interrupted
-
assertEmpty
Assert that theTestObserver
/TestSubscriber
has received aDisposable
/Subscription
viaonSubscribe
but no other events.- Returns:
- this
-
withTag
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
- Since:
- 2.1
-
awaitCount
Await until theTestObserver
/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
- Since:
- 2.1
-
isDisposed
protected abstract boolean isDisposed()Returns true if this test consumer was cancelled/disposed.- Returns:
- true if this test consumer was cancelled/disposed.
-
dispose
protected abstract void dispose()Cancel/dispose this test consumer.
-