public 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()
|
TestSubscriber(long initialRequest)
Constructs a TestSubscriber with the initial request to be requested from upstream.
|
TestSubscriber(Observer
|
TestSubscriber(Observer
Constructs a TestSubscriber with the initial request to be requested from upstream and a delegate Observer to wrap.
|
TestSubscriber(Subscriber
|
| Modifier and Type | Method and Description |
|---|---|
void |
assertCompleted()
Asserts that there is exactly one completion event.
|
void |
assertError(Class
Asserts that there is exactly one error event which is a subclass of the given class.
|
void |
assertError(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(List
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 |
awaitTerminalEvent()
Blocks until this
Subscriber receives a notification that the
Observable is complete (either an
onCompleted or
onError notification).
|
void |
awaitTerminalEvent(long timeout, TimeUnit
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, TimeUnit
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).
|
static <T> TestSubscriber |
create()
|
static <T> TestSubscriber |
create(long initialRequest)
|
static <T> TestSubscriber |
create(Observer
|
static <T> TestSubscriber |
create(Observer
|
static <T> TestSubscriber |
create(Subscriber
|
Thread |
getLastSeenThread()
Returns the last thread that was in use when an item or notification was received by this
Subscriber.
|
List |
getOnCompletedEvents()
Returns the
Notifications representing each time this
Subscriber was notified of sequence completion via
onCompleted(), as a
List.
|
List |
getOnErrorEvents()
|
List |
getOnNextEvents()
Returns the sequence of items observed by this
Subscriber, as an ordered
List.
|
void |
onCompleted()
Notifies the Subscriber that the
Observable has finished sending push-based notifications.
|
void |
onError(Throwable
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 |
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.
|
void |
requestMore(long n)
Allows calling the protected
Subscriber from unit tests.
|
add, isUnsubscribed, request, setProducer, unsubscribe@Experimental public TestSubscriber(long initialRequest)
initialRequest - the initial request value, negative value will revert to the default unbounded behavior
@Experimental public TestSubscriber(Observer<T> delegate, long initialRequest)
initialRequest - the initial request value, negative value will revert to the default unbounded behavior
delegate - the Observer instance to wrap
public TestSubscriber(Subscriber<T> delegate)
public TestSubscriber()
@Experimental public static <T> TestSubscriber<T> create()
@Experimental public static <T> TestSubscriber<T> create(long initialRequest)
@Experimental public static <T> TestSubscriber<T> create(Observer <T> delegate, long initialRequest)
@Experimental public static <T> TestSubscriber<T> create(Subscriber <T> delegate)
@Experimental public static <T> TestSubscriber<T> create(Observer <T> delegate)
public void onStart()
Subscriber
public void onCompleted()
Observable has finished sending push-based notifications.
The Observable will not call this method if it calls onError(java.lang.Throwable).
public List<Notification <T>> getOnCompletedEvents()
Notifications representing each time this
Subscriber was notified of sequence completion via
onCompleted(), as a
List.
onCompleted() method
public void onError(Throwablee)
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 Observable
public List<Throwable > getOnErrorEvents()
onError(java.lang.Throwable) method
public 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 Observable
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 pace
public List<T> getOnNextEvents()
Subscriber, as an ordered
List.
public void assertReceivedOnNext(List<T> items)
Subscriber in order.
items - the sequence of items expected to have been observed
AssertionError - if the sequence of items observed does not exactly match
items
public void assertTerminalEvent()
onCompleted() or
onError(java.lang.Throwable).
AssertionError - if not exactly one terminal event notification was received
public void assertUnsubscribed()
Subscriber is unsubscribed.
AssertionError - if this
Subscriber is not unsubscribed
public void assertNoErrors()
Subscriber has received no
onError notifications.
AssertionError - if this
Subscriber has received one or more
onError notifications
public void awaitTerminalEvent()
Subscriber receives a notification that the
Observable is complete (either an
onCompleted or
onError notification).
RuntimeException - if the Subscriber is interrupted before the Observable is able to complete
public void awaitTerminalEvent(long timeout,
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 timeout
unit - the units in which
timeout is expressed
RuntimeException - if the Subscriber is interrupted before the Observable is able to complete
public void awaitTerminalEventAndUnsubscribeOnTimeout(long timeout,
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 timeout
unit - the units in which
timeout is expressed
public ThreadgetLastSeenThread()
Subscriber.
Thread on which this Subscriber last received an item or notification from the Observable it is subscribed to
@Experimental public void assertCompleted()
AssertionError - if there were zero, or more than one, onCompleted events
@Experimental public void assertNotCompleted()
AssertionError - if there were one or more than one onCompleted events
@Experimental public void assertError(Class<? extends Throwable > clazz)
clazz - the class to check the error against.
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 class
@Experimental public void assertError(Throwablethrowable)
throwable - the throwable to check
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 throwable
@Experimental public void assertNoTerminalEvent()
AssertionError - if there was either an onError or onCompleted event
@Experimental public void assertNoValues()
AssertionError - if there were any onNext events
@Experimental public void assertValueCount(int count)
count - the expected number of onNext events
AssertionError - if there were more or fewer onNext events than specified by
count
@Experimental public void assertValues(T... values)
values - the items to check
AssertionError - if the items emitted do not exactly match those specified by
values
@Experimental public void assertValue(T value)
value - the item to check
AssertionError - if the Observable does not emit only the single item specified by
value