T
- the value typepublic class AssertableSubscriberObservable<T> extends Subscriber<T> implements AssertableSubscriber<T>
AssertableSubscriber
is a variety of Subscriber
that you can use
for unit testing, to perform assertions or inspect received events.
AssertableSubscriber is a duplicate of TestSubscriber but supports method chaining
where possible.Constructor and Description |
---|
AssertableSubscriberObservable(TestSubscriber<T> ts) |
Modifier and Type | Method and Description |
---|---|
AssertableSubscriber<T> |
assertCompleted()
Assert that this Observer received exaclty one
onCompleted signal. |
AssertableSubscriber<T> |
assertError(Class<? extends Throwable> clazz)
Assert that this Observer received one
onError signal with
the given subclass of a Throwable as type. |
AssertableSubscriber<T> |
assertError(Throwable throwable)
Assert that this Observer received one
onError signal with the
object-equals of the given Throwable instance |
AssertableSubscriber<T> |
assertFailure(Class<? extends Throwable> errorClass,
T... values)
Assert that this Observer received the specified items in the given order followed
by an error signal of the given type (but no completion signal).
|
AssertableSubscriber<T> |
assertFailureAndMessage(Class<? extends Throwable> errorClass,
String message,
T... values)
Assert that this Observer received the specified items in the given order followed
by an error signal of the given type and with the exact error message (but no completion signal).
|
AssertableSubscriber<T> |
assertNoErrors()
Assert that this Observer has not received any
onError signal. |
AssertableSubscriber<T> |
assertNotCompleted()
Assert that this Observer received no
onCompleted signal. |
AssertableSubscriber<T> |
assertNoTerminalEvent()
Assert that no
onError or onCompleted signals were received so far. |
AssertableSubscriber<T> |
assertNoValues()
Assert that no
onNext signals were received so far. |
AssertableSubscriber<T> |
assertReceivedOnNext(List<T> items)
Assert that this Observer received the given list of items as
onNext signals
in the same order and with the default null-safe object equals comparison. |
AssertableSubscriber<T> |
assertResult(T... values)
Assert that this Observer received the specified items in the given order followed
by a completion signal and no errors.
|
AssertableSubscriber<T> |
assertTerminalEvent()
Assert that this Observer received either an
onError or onCompleted signal. |
AssertableSubscriber<T> |
assertUnsubscribed()
Assert that this Observer has been unsubscribed via
unsubscribe() or by a wrapping
SafeSubscriber . |
AssertableSubscriber<T> |
assertValue(T value)
Assert that this Observer received exactly the given single expected value
(compared via null-safe object equals).
|
AssertableSubscriber<T> |
assertValueCount(int count)
Assert that this Observer received exactly the given count of
onNext signals. |
AssertableSubscriber<T> |
assertValues(T... values)
Assert that this Observer received exactly the given expected values
(compared via null-safe object equals) in the given order.
|
AssertableSubscriber<T> |
assertValuesAndClear(T expectedFirstValue,
T... expectedRestValues)
Assert that this Observer received exactly the given values (compared via
null-safe object equals) and if so, clears the internal buffer of the
underlying Subscriber of these values.
|
AssertableSubscriber<T> |
awaitTerminalEvent()
Waits for an
onError or {code onCompleted} terminal event indefinitely. |
AssertableSubscriber<T> |
awaitTerminalEvent(long timeout,
TimeUnit unit)
Waits for an
onError or {code onCompleted} terminal event for the given
amount of timeout. |
AssertableSubscriber<T> |
awaitTerminalEventAndUnsubscribeOnTimeout(long timeout,
TimeUnit unit)
Waits for an
onError or {code onCompleted} terminal event for the given
amount of timeout and unsubscribes the sequence if the timeout passed or the
wait itself is interrupted. |
AssertableSubscriber<T> |
awaitValueCount(int expected,
long timeout,
TimeUnit unit)
Assert that this Observer receives at least the given number of
onNext
signals within the specified timeout period. |
static <T> AssertableSubscriberObservable<T> |
create(long initialRequest) |
int |
getCompletions()
Returns the number of
onCompleted signals received by this Observer. |
Thread |
getLastSeenThread()
Returns the Thread that has called the last
onNext , onError or
onCompleted methods of this Observer. |
List<Throwable> |
getOnErrorEvents()
Returns a list of received
onError signals. |
List<T> |
getOnNextEvents()
Returns the list of received
onNext events. |
int |
getValueCount()
Returns the number of
onNext signals received by this Observer in
a thread-safe manner; one can read up to this number of elements from
the List returned by AssertableSubscriber.getOnNextEvents() . |
void |
onCompleted()
Notifies the Observer that the
Observable has finished sending push-based notifications. |
void |
onError(Throwable e)
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 |
onStart()
This method is invoked when the Subscriber and Observable have been connected but the Observable has
not yet begun to emit items or send notifications to the Subscriber.
|
AssertableSubscriber<T> |
perform(Action0 action)
Performs an action given by the Action0 callback in a fluent manner.
|
AssertableSubscriber<T> |
requestMore(long n)
Requests the specified amount of items from upstream.
|
void |
setProducer(Producer p)
If other subscriber is set (by calling constructor
Subscriber.Subscriber(Subscriber) or
Subscriber.Subscriber(Subscriber, boolean) ) then this method calls
setProducer on the other subscriber. |
String |
toString() |
add, isUnsubscribed, request, unsubscribe
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
isUnsubscribed, unsubscribe
public AssertableSubscriberObservable(TestSubscriber<T> ts)
public static <T> AssertableSubscriberObservable<T> create(long initialRequest)
public void onStart()
Subscriber
onStart
in interface AssertableSubscriber<T>
onStart
in class Subscriber<T>
public void onCompleted()
Observer
Observable
has finished sending push-based notifications.
The Observable
will not call this method if it calls Observer.onError(java.lang.Throwable)
.
onCompleted
in interface Observer<T>
public void setProducer(Producer p)
Subscriber
Subscriber.Subscriber(Subscriber)
or
Subscriber.Subscriber(Subscriber, boolean)
) then this method calls
setProducer
on the other subscriber. If the other subscriber
is not set and no requests have been made to this subscriber then
p.request(Long.MAX_VALUE)
is called. If the other subscriber
is not set and some requests have been made to this subscriber then
p.request(n)
is called where n is the accumulated requests
to this subscriber.setProducer
in interface AssertableSubscriber<T>
setProducer
in class Subscriber<T>
p
- producer to be used by this subscriber or the other subscriber
(or recursively its other subscriber) to make requests frompublic final int getCompletions()
AssertableSubscriber
onCompleted
signals received by this Observer.getCompletions
in interface AssertableSubscriber<T>
onCompleted
signals receivedpublic void onError(Throwable e)
Observer
Observable
has experienced an error condition.
If the Observable
calls this method, it will not thereafter call Observer.onNext(T)
or
Observer.onCompleted()
.
public List<Throwable> getOnErrorEvents()
AssertableSubscriber
onError
signals.getOnErrorEvents
in interface AssertableSubscriber<T>
public 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.onCompleted()
or
Observer.onError(java.lang.Throwable)
.
public final int getValueCount()
AssertableSubscriber
onNext
signals received by this Observer in
a thread-safe manner; one can read up to this number of elements from
the List
returned by AssertableSubscriber.getOnNextEvents()
.getValueCount
in interface AssertableSubscriber<T>
onNext
signals received.public AssertableSubscriber<T> requestMore(long n)
AssertableSubscriber
requestMore
in interface AssertableSubscriber<T>
n
- the amount requested, non-negativepublic List<T> getOnNextEvents()
AssertableSubscriber
onNext
events.
If the sequence hasn't completed yet and is asynchronous, use the
AssertableSubscriber.getValueCount()
method to determine how many elements are safe
to be read from the list returned by this method.
getOnNextEvents
in interface AssertableSubscriber<T>
onNext
events.public AssertableSubscriber<T> assertReceivedOnNext(List<T> items)
AssertableSubscriber
onNext
signals
in the same order and with the default null-safe object equals comparison.assertReceivedOnNext
in interface AssertableSubscriber<T>
items
- the List of items expectedpublic final AssertableSubscriber<T> awaitValueCount(int expected, long timeout, TimeUnit unit)
AssertableSubscriber
onNext
signals within the specified timeout period.
Note that it is possible the AssertionError thrown by this method will contain an actual value >= to the expected one in case there is an emission race or unexpected delay on the emitter side. In this case, increase the timeout amount to avoid false positives.
awaitValueCount
in interface AssertableSubscriber<T>
expected
- the expected (at least) number of onNext
signalstimeout
- the timeout to wait to receive the given number of onNext
eventsunit
- the time unitpublic AssertableSubscriber<T> assertTerminalEvent()
AssertableSubscriber
onError
or onCompleted
signal.assertTerminalEvent
in interface AssertableSubscriber<T>
public AssertableSubscriber<T> assertUnsubscribed()
AssertableSubscriber
unsubscribe()
or by a wrapping
SafeSubscriber
.assertUnsubscribed
in interface AssertableSubscriber<T>
public AssertableSubscriber<T> assertNoErrors()
AssertableSubscriber
onError
signal.assertNoErrors
in interface AssertableSubscriber<T>
public AssertableSubscriber<T> awaitTerminalEvent()
AssertableSubscriber
onError
or {code onCompleted} terminal event indefinitely.awaitTerminalEvent
in interface AssertableSubscriber<T>
public AssertableSubscriber<T> awaitTerminalEvent(long timeout, TimeUnit unit)
AssertableSubscriber
onError
or {code onCompleted} terminal event for the given
amount of timeout.awaitTerminalEvent
in interface AssertableSubscriber<T>
timeout
- the time to wait for the terminal eventunit
- the time unit of the wait timepublic AssertableSubscriber<T> awaitTerminalEventAndUnsubscribeOnTimeout(long timeout, TimeUnit unit)
AssertableSubscriber
onError
or {code onCompleted} terminal event for the given
amount of timeout and unsubscribes the sequence if the timeout passed or the
wait itself is interrupted.awaitTerminalEventAndUnsubscribeOnTimeout
in interface AssertableSubscriber<T>
timeout
- the time to wait for the terminal eventunit
- the time unit of the wait timepublic Thread getLastSeenThread()
AssertableSubscriber
onNext
, onError
or
onCompleted
methods of this Observer.getLastSeenThread
in interface AssertableSubscriber<T>
public AssertableSubscriber<T> assertCompleted()
AssertableSubscriber
onCompleted
signal.assertCompleted
in interface AssertableSubscriber<T>
public AssertableSubscriber<T> assertNotCompleted()
AssertableSubscriber
onCompleted
signal.assertNotCompleted
in interface AssertableSubscriber<T>
public AssertableSubscriber<T> assertError(Class<? extends Throwable> clazz)
AssertableSubscriber
onError
signal with
the given subclass of a Throwable as type.assertError
in interface AssertableSubscriber<T>
clazz
- the expected type of the onError
signal receivedpublic AssertableSubscriber<T> assertError(Throwable throwable)
AssertableSubscriber
onError
signal with the
object-equals of the given Throwable instanceassertError
in interface AssertableSubscriber<T>
throwable
- the Throwable instance expectedpublic AssertableSubscriber<T> assertNoTerminalEvent()
AssertableSubscriber
onError
or onCompleted
signals were received so far.assertNoTerminalEvent
in interface AssertableSubscriber<T>
public AssertableSubscriber<T> assertNoValues()
AssertableSubscriber
onNext
signals were received so far.assertNoValues
in interface AssertableSubscriber<T>
public AssertableSubscriber<T> assertValueCount(int count)
AssertableSubscriber
onNext
signals.assertValueCount
in interface AssertableSubscriber<T>
count
- the expected number of onNext
signalspublic AssertableSubscriber<T> assertValues(T... values)
AssertableSubscriber
assertValues
in interface AssertableSubscriber<T>
values
- the expected valuespublic AssertableSubscriber<T> assertValue(T value)
AssertableSubscriber
assertValue
in interface AssertableSubscriber<T>
value
- the single value expectedpublic final AssertableSubscriber<T> assertValuesAndClear(T expectedFirstValue, T... expectedRestValues)
AssertableSubscriber
assertValuesAndClear
in interface AssertableSubscriber<T>
expectedFirstValue
- the first value expectedexpectedRestValues
- the rest of the values expectedpublic final AssertableSubscriber<T> perform(Action0 action)
AssertableSubscriber
perform
in interface AssertableSubscriber<T>
action
- the action to perform, not nullpublic final AssertableSubscriber<T> assertResult(T... values)
AssertableSubscriber
assertResult
in interface AssertableSubscriber<T>
values
- the values expectedpublic final AssertableSubscriber<T> assertFailure(Class<? extends Throwable> errorClass, T... values)
AssertableSubscriber
assertFailure
in interface AssertableSubscriber<T>
errorClass
- the expected Throwable subclass typevalues
- the expected valuespublic final AssertableSubscriber<T> assertFailureAndMessage(Class<? extends Throwable> errorClass, String message, T... values)
AssertableSubscriber
assertFailureAndMessage
in interface AssertableSubscriber<T>
errorClass
- the expected Throwable subclass typemessage
- the expected error message returned by Throwable.getMessage()
values
- the expected valuesCopyright © 2017. All rights reserved.