T
- the value typepublic class TestObserver<T> extends java.lang.Object implements Observer<T>, Disposable, MaybeObserver<T>, SingleObserver<T>, CompletableObserver
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.
Constructor and Description |
---|
TestObserver()
Constructs a non-forwarding TestObserver.
|
TestObserver(Observer<? super T> actual)
Constructs a forwarding TestObserver.
|
Modifier and Type | Method and Description |
---|---|
TestObserver<T> |
assertComplete()
Assert that this TestObserver received exactly one onComplete event.
|
TestObserver<T> |
assertEmpty()
Assert that the TestObserver has received a Disposable but no other events.
|
TestObserver<T> |
assertError(java.lang.Class<? extends java.lang.Throwable> errorClass)
Asserts that this TestObserver received exactly one onError event which is an
instance of the specified errorClass class.
|
TestObserver<T> |
assertError(Predicate<java.lang.Throwable> errorPredicate)
Asserts that this TestObserver received exactly one onError event for which
the provided predicate returns true.
|
TestObserver<T> |
assertError(java.lang.Throwable error)
Assert that this TestObserver received exactly the specified onError event value.
|
TestObserver<T> |
assertErrorMessage(java.lang.String message)
Assert that there is a single error and it has the given message.
|
TestObserver<T> |
assertFailure(java.lang.Class<? extends java.lang.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.
|
TestObserver<T> |
assertFailure(Predicate<java.lang.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.
|
TestObserver<T> |
assertFailureAndMessage(java.lang.Class<? extends java.lang.Throwable> error,
java.lang.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.
|
TestObserver<T> |
assertNoErrors()
Assert that this TestObserver has not received any onError event.
|
TestObserver<T> |
assertNotComplete()
Assert that this TestObserver has not received any onComplete event.
|
TestObserver<T> |
assertNotSubscribed()
Assert that the onSubscribe method hasn't been called at all.
|
TestObserver<T> |
assertNotTerminated()
Assert that the TestObserver has not terminated (i.e., the terminal latch is still non-zero).
|
TestObserver<T> |
assertNoValues()
Assert that this TestObserver has not received any onNext events.
|
TestObserver<T> |
assertOf(Consumer<? super TestObserver<T>> check)
Run a check consumer with this TestObserver instance.
|
TestObserver<T> |
assertResult(T... values)
Assert that the upstream signalled the specified values in order and
completed normally.
|
TestObserver<T> |
assertSubscribed()
Assert that the onSubscribe method was called exactly once.
|
TestObserver<T> |
assertTerminated()
Assert that the TestObserver terminated (i.e., the terminal latch reached zero).
|
TestObserver<T> |
assertValue(T value)
Assert that this TestObserver received exactly one onNext value which is equal to
the given value with respect to Objects.equals.
|
TestObserver<T> |
assertValueCount(int count)
Assert that this TestObserver received the specified number onNext events.
|
TestObserver<T> |
assertValues(T... values)
Assert that the TestObserver received only the specified values in the specified order.
|
TestObserver<T> |
assertValueSequence(java.lang.Iterable<? extends T> sequence)
Assert that the TestObserver received only the specified sequence of values in the same order.
|
TestObserver<T> |
assertValueSet(java.util.Collection<? extends T> expected)
Assert that the TestObserver received only the specified values in any order.
|
TestObserver<T> |
await()
Awaits until this TestObserver receives an onError or onComplete events.
|
boolean |
await(long time,
java.util.concurrent.TimeUnit unit)
Awaits the specified amount of time or until this TestObserver
receives an onError or onComplete events, whichever happens first.
|
TestObserver<T> |
awaitDone(long time,
java.util.concurrent.TimeUnit unit)
Awaits until the internal latch is counted down.
|
boolean |
awaitTerminalEvent()
Waits until the any terminal event has been received by this TestObserver
or returns false if the wait has been interrupted.
|
boolean |
awaitTerminalEvent(long duration,
java.util.concurrent.TimeUnit unit)
Awaits the specified amount of time or until this TestObserver
receives an onError or onComplete events, whichever happens first.
|
void |
cancel()
Cancels the TestObserver (before or after the subscription happened).
|
long |
completions()
Returns the number of times onComplete was called.
|
static <T> TestObserver<T> |
create()
Constructs a non-forwarding TestObserver.
|
static <T> TestObserver<T> |
create(Observer<? super T> delegate)
Constructs a forwarding TestObserver.
|
void |
dispose()
Dispose the resource, the operation should be idempotent.
|
int |
errorCount()
Returns the number of onError exceptions received.
|
java.util.List<java.lang.Throwable> |
errors()
Returns a shared list of received onError exceptions.
|
java.util.List<java.util.List<java.lang.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.
|
boolean |
hasSubscription()
Returns true if this TestObserver received a subscription.
|
boolean |
isCancelled()
Returns true if this TestObserver has been cancelled.
|
boolean |
isDisposed()
Returns true if this resource has been disposed.
|
boolean |
isTerminated()
Returns true if TestObserver received any onError or onComplete events.
|
java.lang.Thread |
lastThread()
Returns the last thread which called the onXXX methods of this TestObserver.
|
void |
onComplete()
Notifies the Observer that the
Observable has finished sending push-based notifications. |
void |
onError(java.lang.Throwable t)
Notifies the Observer that the
Observable has experienced an error condition. |
void |
onNext(T t)
Provides the Observer with a new item to observe.
|
void |
onSubscribe(Disposable s)
Provides the Observer with the means of cancelling (disposing) the
connection (channel) with the Observable in both
synchronous (from within
Observer.onNext(Object) ) and asynchronous manner. |
void |
onSuccess(T value)
Notifies the MaybeObserver with one item and that the
Maybe has finished sending
push-based notifications. |
int |
valueCount()
Returns the number of onNext values received.
|
java.util.List<T> |
values()
Returns a shared list of received onNext values.
|
public static <T> TestObserver<T> create()
T
- the value type receivedpublic static <T> TestObserver<T> create(Observer<? super T> delegate)
T
- the value type receiveddelegate
- the actual Observer to forward events topublic void onSubscribe(Disposable s)
Observer
Observer.onNext(Object)
) and asynchronous manner.onSubscribe
in interface CompletableObserver
onSubscribe
in interface MaybeObserver<T>
onSubscribe
in interface Observer<T>
onSubscribe
in interface SingleObserver<T>
s
- the Disposable instance whose Disposable.dispose()
can
be called anytime to cancel the connectionpublic void onNext(T t)
Observer
The Observable
may call this method 0 or more times.
The Observable
will not call this method again after it calls either Observer.onComplete()
or
Observer.onError(java.lang.Throwable)
.
public void onError(java.lang.Throwable t)
Observer
Observable
has experienced an error condition.
If the Observable
calls this method, it will not thereafter call Observer.onNext(T)
or
Observer.onComplete()
.
onError
in interface CompletableObserver
onError
in interface MaybeObserver<T>
onError
in interface Observer<T>
onError
in interface SingleObserver<T>
t
- the exception encountered by the Observablepublic void onComplete()
Observer
Observable
has finished sending push-based notifications.
The Observable
will not call this method if it calls Observer.onError(java.lang.Throwable)
.
onComplete
in interface CompletableObserver
onComplete
in interface MaybeObserver<T>
onComplete
in interface Observer<T>
public final boolean isCancelled()
public final void cancel()
This operation is thread-safe.
This method is provided as a convenience when converting Flowable tests that cancel.
public final void dispose()
Disposable
dispose
in interface Disposable
public final boolean isDisposed()
Disposable
isDisposed
in interface Disposable
public final java.lang.Thread lastThread()
public final java.util.List<T> values()
public final java.util.List<java.lang.Throwable> errors()
public final long completions()
public final boolean isTerminated()
public final int valueCount()
public final int errorCount()
public final boolean hasSubscription()
public final TestObserver<T> await() throws java.lang.InterruptedException
java.lang.InterruptedException
- if the current thread is interrupted while waitingawaitTerminalEvent()
public final boolean await(long time, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
time
- the waiting timeunit
- the time unit of the waiting timejava.lang.InterruptedException
- if the current thread is interrupted while waitingawaitTerminalEvent(long, TimeUnit)
public final TestObserver<T> assertComplete()
public final TestObserver<T> assertNotComplete()
public final TestObserver<T> assertNoErrors()
public final TestObserver<T> assertError(java.lang.Throwable error)
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.
error
- the error to checkassertError(Class)
,
assertError(Predicate)
public final TestObserver<T> assertError(java.lang.Class<? extends java.lang.Throwable> errorClass)
errorClass
- the error class to expectpublic final TestObserver<T> assertError(Predicate<java.lang.Throwable> errorPredicate)
errorPredicate
- the predicate that receives the error Throwable
and should return true for expected errors.public final TestObserver<T> assertValue(T value)
value
- the value to expectpublic final TestObserver<T> assertValueCount(int count)
count
- the expected number of onNext eventspublic final TestObserver<T> assertNoValues()
public final TestObserver<T> assertValues(T... values)
values
- the values expectedassertValueSet(Collection)
public final TestObserver<T> assertValueSet(java.util.Collection<? extends T> expected)
This helps asserting when the order of the values is not guaranteed, i.e., when merging asynchronous streams.
expected
- the collection of values expected in any orderpublic final TestObserver<T> assertValueSequence(java.lang.Iterable<? extends T> sequence)
sequence
- the sequence of expected values in orderpublic final TestObserver<T> assertTerminated()
public final TestObserver<T> assertNotTerminated()
public final TestObserver<T> assertSubscribed()
public final TestObserver<T> assertNotSubscribed()
public final boolean awaitTerminalEvent()
public final boolean awaitTerminalEvent(long duration, java.util.concurrent.TimeUnit unit)
duration
- the waiting timeunit
- the time unit of the waiting timepublic final TestObserver<T> assertErrorMessage(java.lang.String message)
message
- the message expectedpublic final java.util.List<java.util.List<java.lang.Object>> getEvents()
public final TestObserver<T> assertOf(Consumer<? super TestObserver<T>> check)
check
- the check consumer to runpublic final TestObserver<T> assertResult(T... values)
values
- the expected values, asserted in orderassertFailure(Class, Object...)
,
assertFailure(Predicate, Object...)
,
assertFailureAndMessage(Class, String, Object...)
public final TestObserver<T> assertFailure(java.lang.Class<? extends java.lang.Throwable> error, T... values)
error
- the expected exception (parent) classvalues
- the expected values, asserted in orderpublic final TestObserver<T> assertFailure(Predicate<java.lang.Throwable> errorPredicate, T... values)
errorPredicate
- the predicate that receives the error Throwable
and should return true for expected errors.values
- the expected values, asserted in orderpublic final TestObserver<T> assertFailureAndMessage(java.lang.Class<? extends java.lang.Throwable> error, java.lang.String message, T... values)
error
- the expected exception (parent) classmessage
- the expected failure messagevalues
- the expected values, asserted in orderpublic final TestObserver<T> awaitDone(long time, java.util.concurrent.TimeUnit unit)
If the wait times out or gets interrupted, the TestObserver is cancelled.
time
- the waiting timeunit
- the time unit of the waiting timejava.lang.RuntimeException
- wrapping an InterruptedException if the wait is interruptedpublic final TestObserver<T> assertEmpty()
public void onSuccess(T value)
MaybeObserver
Maybe
has finished sending
push-based notifications.
The Maybe
will not call this method if it calls MaybeObserver.onError(java.lang.Throwable)
.
onSuccess
in interface MaybeObserver<T>
onSuccess
in interface SingleObserver<T>
value
- the item emitted by the Maybe