Class TestSubscriber<T>

  • All Implemented Interfaces:
    Observer<T>, Subscription


    public class TestSubscriber<T>
    extends Subscriber<T>
    A 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 Detail

      • TestSubscriber

        @Experimental
        public TestSubscriber(long initialRequest)
        Constructs a TestSubscriber with the initial request to be requested from upstream.
        Parameters:
        initialRequest - the initial request value, negative value will revert to the default unbounded behavior
        Since:
        (if this graduates from "Experimental" replace this parenthetical with the release number)
      • TestSubscriber

        @Experimental
        public TestSubscriber(Observer<T> delegate,
                                            long initialRequest)
        Constructs a TestSubscriber with the initial request to be requested from upstream and a delegate Observer to wrap.
        Parameters:
        initialRequest - the initial request value, negative value will revert to the default unbounded behavior
        delegate - the Observer instance to wrap
        Since:
        (if this graduates from "Experimental" replace this parenthetical with the release number)
      • TestSubscriber

        public TestSubscriber(Subscriber<T> delegate)
      • TestSubscriber

        public TestSubscriber(Observer<T> delegate)
      • TestSubscriber

        public TestSubscriber()
    • Method Detail

      • onStart

        public void onStart()
        Description copied from class: Subscriber
        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. Override this method to add any useful initialization to your subscription, for instance to initiate backpressure.
      • onCompleted

        public void onCompleted()
        Notifies the Subscriber that the Observable has finished sending push-based notifications.

        The Observable will not call this method if it calls onError(java.lang.Throwable).

      • onError

        public void onError(Throwable e)
        Notifies the Subscriber that the Observable has experienced an error condition.

        If the Observable calls this method, it will not thereafter call onNext(T) or onCompleted().

        Parameters:
        e - the exception encountered by the Observable
      • onNext

        public void onNext(T t)
        Provides the Subscriber with a new item to observe.

        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).

        Parameters:
        t - the item emitted by the Observable
      • requestMore

        public void requestMore(long n)
        Allows calling the protected Subscriber.request(long) from unit tests.
        Parameters:
        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
      • getOnNextEvents

        public List<T> getOnNextEvents()
        Returns the sequence of items observed by this Subscriber, as an ordered List.
        Returns:
        a list of items observed by this Subscriber, in the order in which they were observed
      • assertReceivedOnNext

        public void assertReceivedOnNext(List<T> items)
        Asserts that a particular sequence of items was received by this Subscriber in order.
        Parameters:
        items - the sequence of items expected to have been observed
        Throws:
        AssertionError - if the sequence of items observed does not exactly match items
      • assertUnsubscribed

        public void assertUnsubscribed()
        Asserts that this Subscriber is unsubscribed.
        Throws:
        AssertionError - if this Subscriber is not unsubscribed
      • assertNoErrors

        public void assertNoErrors()
        Asserts that this Subscriber has received no onError notifications.
        Throws:
        AssertionError - if this Subscriber has received one or more onError notifications
      • awaitTerminalEvent

        public void awaitTerminalEvent()
        Blocks until this Subscriber receives a notification that the Observable is complete (either an onCompleted or onError notification).
        Throws:
        RuntimeException - if the Subscriber is interrupted before the Observable is able to complete
      • awaitTerminalEvent

        public void awaitTerminalEvent(long timeout,
                                       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.
        Parameters:
        timeout - the duration of the timeout
        unit - the units in which timeout is expressed
        Throws:
        RuntimeException - if the Subscriber is interrupted before the Observable is able to complete
      • awaitTerminalEventAndUnsubscribeOnTimeout

        public void awaitTerminalEventAndUnsubscribeOnTimeout(long timeout,
                                                              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). If timeout expires then the Subscriber is unsubscribed from the Observable.
        Parameters:
        timeout - the duration of the timeout
        unit - the units in which timeout is expressed
      • getLastSeenThread

        public Thread getLastSeenThread()
        Returns the last thread that was in use when an item or notification was received by this Subscriber.
        Returns:
        the Thread on which this Subscriber last received an item or notification from the Observable it is subscribed to
      • assertCompleted

        @Experimental
        public void assertCompleted()
        Asserts that there is exactly one completion event.
        Throws:
        AssertionError - if there were zero, or more than one, onCompleted events
        Since:
        (if this graduates from "Experimental" replace this parenthetical with the release number)
      • assertNotCompleted

        @Experimental
        public void assertNotCompleted()
        Asserts that there is no completion event.
        Throws:
        AssertionError - if there were one or more than one onCompleted events
        Since:
        (if this graduates from "Experimental" replace this parenthetical with the release number)
      • assertError

        @Experimental
        public void assertError(Class<? extends Throwable> clazz)
        Asserts that there is exactly one error event which is a subclass of the given class.
        Parameters:
        clazz - the class to check the error against.
        Throws:
        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
        Since:
        (if this graduates from "Experimental" replace this parenthetical with the release number)
      • assertError

        @Experimental
        public void assertError(Throwable throwable)
        Asserts that there is a single onError event with the exact exception.
        Parameters:
        throwable - the throwable to check
        Throws:
        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
        Since:
        (if this graduates from "Experimental" replace this parenthetical with the release number)
      • assertNoTerminalEvent

        @Experimental
        public void assertNoTerminalEvent()
        Asserts that there are no onError and onCompleted events.
        Throws:
        AssertionError - if there was either an onError or onCompleted event
        Since:
        (if this graduates from "Experimental" replace this parenthetical with the release number)
      • assertNoValues

        @Experimental
        public void assertNoValues()
        Asserts that there are no onNext events received.
        Throws:
        AssertionError - if there were any onNext events
        Since:
        (if this graduates from "Experimental" replace this parenthetical with the release number)
      • assertValueCount

        @Experimental
        public void assertValueCount(int count)
        Asserts that the given number of onNext events are received.
        Parameters:
        count - the expected number of onNext events
        Throws:
        AssertionError - if there were more or fewer onNext events than specified by count
        Since:
        (if this graduates from "Experimental" replace this parenthetical with the release number)
      • assertValues

        @Experimental
        public void assertValues(T... values)
        Asserts that the received onNext events, in order, are the specified items.
        Parameters:
        values - the items to check
        Throws:
        AssertionError - if the items emitted do not exactly match those specified by values
        Since:
        (if this graduates from "Experimental" replace this parenthetical with the release number)
      • assertValue

        @Experimental
        public void assertValue(T value)
        Asserts that there is only a single received onNext event and that it marks the emission of a specific item.
        Parameters:
        value - the item to check
        Throws:
        AssertionError - if the Observable does not emit only the single item specified by value
        Since:
        (if this graduates from "Experimental" replace this parenthetical with the release number)