T - the value typepublic class TestSubscriber<T> extends Subscriber<T>
TestSubscriber is a variety of Subscriber that you can use for unit testing, to perform
assertions, inspect received events, or wrap a mocked Subscriber.| Constructor and Description |
|---|
TestSubscriber()
Constructs a TestSubscriber with an initial request of Long.MAX_VALUE and no delegation.
|
TestSubscriber(long initialRequest)
Constructs a TestSubscriber with the initial request to be requested from upstream.
|
TestSubscriber(Observer<T> delegate)
Constructs a TestSubscriber which requests Long.MAX_VALUE and delegates events to
the given Observer.
|
TestSubscriber(Observer<T> delegate,
long initialRequest)
Constructs a TestSubscriber with the initial request to be requested from upstream
and a delegate Observer to wrap.
|
TestSubscriber(Subscriber<T> delegate)
Constructs a TestSubscriber which requests Long.MAX_VALUE and delegates events to
the given Subscriber.
|
| Modifier and Type | Method and Description |
|---|---|
void |
assertCompleted()
Asserts that there is exactly one completion event.
|
void |
assertError(java.lang.Class<? extends java.lang.Throwable> clazz)
Asserts that there is exactly one error event which is a subclass of the given class.
|
void |
assertError(java.lang.Throwable throwable)
Asserts that there is a single onError event with the exact exception.
|
void |
assertNoErrors()
Asserts that this
Subscriber has received no onError notifications. |
void |
assertNotCompleted()
Asserts that there is no completion event.
|
void |
assertNoTerminalEvent()
Asserts that there are no onError and onCompleted events.
|
void |
assertNoValues()
Asserts that there are no onNext events received.
|
void |
assertReceivedOnNext(java.util.List<T> items)
Asserts that a particular sequence of items was received by this
Subscriber in order. |
void |
assertTerminalEvent()
Asserts that a single terminal event occurred, either
onCompleted() or onError(java.lang.Throwable). |
void |
assertUnsubscribed()
Asserts that this
Subscriber is unsubscribed. |
void |
assertValue(T value)
Asserts that there is only a single received onNext event and that it marks the emission of a specific item.
|
void |
assertValueCount(int count)
Asserts that the given number of onNext events are received.
|
void |
assertValues(T... values)
Asserts that the received onNext events, in order, are the specified items.
|
void |
assertValuesAndClear(T expectedFirstValue,
T... expectedRestValues)
Assert that the TestSubscriber contains the given first and optional rest values exactly
and if so, clears the internal list of values.
|
void |
awaitTerminalEvent()
Blocks until this
Subscriber receives a notification that the Observable is complete
(either an onCompleted or onError notification). |
void |
awaitTerminalEvent(long timeout,
java.util.concurrent.TimeUnit unit)
Blocks until this
Subscriber receives a notification that the Observable is complete
(either an onCompleted or onError notification), or until a timeout expires. |
void |
awaitTerminalEventAndUnsubscribeOnTimeout(long timeout,
java.util.concurrent.TimeUnit unit)
Blocks until this
Subscriber receives a notification that the Observable is complete
(either an onCompleted or onError notification), or until a timeout expires; if the
Subscriber is interrupted before either of these events take place, this method unsubscribes the
Subscriber from the Observable). |
boolean |
awaitValueCount(int expected,
long timeout,
java.util.concurrent.TimeUnit unit)
Wait until the current committed value count is less than the expected amount
by sleeping 1 unit at most timeout times and return true if at least
the required amount of onNext values have been received.
|
static <T> TestSubscriber<T> |
create()
Factory method to construct a TestSubscriber with an initial request of Long.MAX_VALUE and no delegation.
|
static <T> TestSubscriber<T> |
create(long initialRequest)
Factory method to construct a TestSubscriber with the given initial request amount and no delegation.
|
static <T> TestSubscriber<T> |
create(Observer<T> delegate)
Factory method to construct a TestSubscriber which delegates events to the given Observer and
an issues an initial request of Long.MAX_VALUE.
|
static <T> TestSubscriber<T> |
create(Observer<T> delegate,
long initialRequest)
Factory method to construct a TestSubscriber which delegates events to the given Observer and
issues the given initial request amount.
|
static <T> TestSubscriber<T> |
create(Subscriber<T> delegate)
Factory method to construct a TestSubscriber which delegates events to the given Subscriber and
an issues an initial request of Long.MAX_VALUE.
|
int |
getCompletions()
Returns the number of times onCompleted was called on this TestSubscriber.
|
java.lang.Thread |
getLastSeenThread()
Returns the last thread that was in use when an item or notification was received by this
Subscriber. |
java.util.List<Notification<T>> |
getOnCompletedEvents()
Deprecated.
use
getCompletions() instead. |
java.util.List<java.lang.Throwable> |
getOnErrorEvents()
|
java.util.List<T> |
getOnNextEvents()
Returns the sequence of items observed by this
Subscriber, as an ordered List. |
int |
getValueCount()
Returns the committed number of onNext elements that are safe to be
read from
getOnNextEvents() other threads. |
void |
onCompleted()
Notifies the Subscriber that the
Observable has finished sending push-based notifications. |
void |
onError(java.lang.Throwable e)
Notifies the Subscriber that the
Observable has experienced an error condition. |
void |
onNext(T t)
Provides the Subscriber with a new item to observe.
|
void |
requestMore(long n)
Allows calling the protected
Subscriber.request(long) from unit tests. |
add, isUnsubscribed, onStart, request, setProducer, unsubscribepublic TestSubscriber(long initialRequest)
initialRequest - the initial request value, negative value will revert to the default unbounded behaviorpublic TestSubscriber(Observer<T> delegate, long initialRequest)
initialRequest - the initial request value, negative value will revert to the default unbounded behaviordelegate - the Observer instance to wrapjava.lang.NullPointerException - if delegate is nullpublic TestSubscriber(Subscriber<T> delegate)
delegate - the subscriber to delegate to.java.lang.NullPointerException - if delegate is nullpublic TestSubscriber(Observer<T> delegate)
delegate - the observer to delegate to.java.lang.NullPointerException - if delegate is nullpublic TestSubscriber()
public static <T> TestSubscriber<T> create()
T - the value typepublic static <T> TestSubscriber<T> create(long initialRequest)
T - the value typeinitialRequest - the initial request amount, negative values revert to the default unbounded modepublic static <T> TestSubscriber<T> create(Observer<T> delegate, long initialRequest)
T - the value typedelegate - the observer to delegate events toinitialRequest - the initial request amount, negative values revert to the default unbounded modejava.lang.NullPointerException - if delegate is nullpublic static <T> TestSubscriber<T> create(Subscriber<T> delegate)
T - the value typedelegate - the subscriber to delegate events tojava.lang.NullPointerException - if delegate is nullpublic static <T> TestSubscriber<T> create(Observer<T> delegate)
T - the value typedelegate - the observer to delegate events tojava.lang.NullPointerException - if delegate is nullpublic void onCompleted()
Observable has finished sending push-based notifications.
The Observable will not call this method if it calls onError(java.lang.Throwable).
@Deprecated public java.util.List<Notification<T>> getOnCompletedEvents()
getCompletions() instead.Notifications representing each time this Subscriber was notified of sequence
completion via onCompleted(), as a List.onCompleted() methodpublic final int getCompletions()
public void onError(java.lang.Throwable e)
Observable has experienced an error condition.
If the Observable calls this method, it will not thereafter call onNext(T) or
onCompleted().
e - the exception encountered by the Observablepublic java.util.List<java.lang.Throwable> getOnErrorEvents()
onError(java.lang.Throwable) methodpublic void onNext(T t)
The Observable may call this method 0 or more times.
The Observable will not call this method again after it calls either onCompleted() or
onError(java.lang.Throwable).
t - the item emitted by the Observablepublic final int getValueCount()
getOnNextEvents() other threads.public void requestMore(long n)
Subscriber.request(long) from unit tests.n - the maximum number of items you want the Observable to emit to the Subscriber at this time, or
Long.MAX_VALUE if you want the Observable to emit items at its own pacepublic java.util.List<T> getOnNextEvents()
Subscriber, as an ordered List.public void assertReceivedOnNext(java.util.List<T> items)
Subscriber in order.items - the sequence of items expected to have been observedjava.lang.AssertionError - if the sequence of items observed does not exactly match itemspublic final boolean awaitValueCount(int expected,
long timeout,
java.util.concurrent.TimeUnit unit)
expected - the expected number of onNext eventstimeout - the time to wait for the eventsunit - the time unit of waitingjava.lang.RuntimeException - if the sleep is interruptedpublic void assertTerminalEvent()
onCompleted() or onError(java.lang.Throwable).java.lang.AssertionError - if not exactly one terminal event notification was receivedpublic void assertUnsubscribed()
Subscriber is unsubscribed.java.lang.AssertionError - if this Subscriber is not unsubscribedpublic void assertNoErrors()
Subscriber has received no onError notifications.java.lang.AssertionError - if this Subscriber has received one or more onError notificationspublic void awaitTerminalEvent()
Subscriber receives a notification that the Observable is complete
(either an onCompleted or onError notification).java.lang.RuntimeException - if the Subscriber is interrupted before the Observable is able to completepublic void awaitTerminalEvent(long timeout,
java.util.concurrent.TimeUnit unit)
Subscriber receives a notification that the Observable is complete
(either an onCompleted or onError notification), or until a timeout expires.timeout - the duration of the timeoutunit - the units in which timeout is expressedjava.lang.RuntimeException - if the Subscriber is interrupted before the Observable is able to completepublic void awaitTerminalEventAndUnsubscribeOnTimeout(long timeout,
java.util.concurrent.TimeUnit unit)
Subscriber receives a notification that the Observable is complete
(either an onCompleted or onError notification), or until a timeout expires; if the
Subscriber is interrupted before either of these events take place, this method unsubscribes the
Subscriber from the Observable). If timeout expires then the Subscriber is unsubscribed from the Observable.timeout - the duration of the timeoutunit - the units in which timeout is expressedpublic java.lang.Thread getLastSeenThread()
Subscriber.Thread on which this Subscriber last received an item or notification from the
Observable it is subscribed topublic void assertCompleted()
java.lang.AssertionError - if there were zero, or more than one, onCompleted eventspublic void assertNotCompleted()
java.lang.AssertionError - if there were one or more than one onCompleted eventspublic void assertError(java.lang.Class<? extends java.lang.Throwable> clazz)
clazz - the class to check the error against.java.lang.AssertionError - if there were zero, or more than one, onError events, or if the single onError
event did not carry an error of a subclass of the given classpublic void assertError(java.lang.Throwable throwable)
throwable - the throwable to checkjava.lang.AssertionError - if there were zero, or more than one, onError events, or if the single onError
event did not carry an error that matches the specified throwablepublic void assertNoTerminalEvent()
java.lang.AssertionError - if there was either an onError or onCompleted eventpublic void assertNoValues()
java.lang.AssertionError - if there were any onNext eventspublic void assertValueCount(int count)
count - the expected number of onNext eventsjava.lang.AssertionError - if there were more or fewer onNext events than specified by countpublic void assertValues(T... values)
values - the items to checkjava.lang.AssertionError - if the items emitted do not exactly match those specified by valuespublic void assertValue(T value)
value - the item to checkjava.lang.AssertionError - if the Observable does not emit only the single item specified by valuepublic final void assertValuesAndClear(T expectedFirstValue, T... expectedRestValues)
TestSubscriber ts = new TestSubscriber();
ts.onNext(1);
ts.assertValuesAndClear(1);
ts.onNext(2);
ts.onNext(3);
ts.assertValuesAndClear(2, 3); // no mention of 1
expectedFirstValue - the expected first valueexpectedRestValues - the optional rest values