Class TestSubscriber<T>
java.lang.Object
io.reactivex.rxjava4.observers.BaseTestConsumer<T, TestSubscriber<T>>
io.reactivex.rxjava4.subscribers.TestSubscriber<T>
- Type Parameters:
T- the value type
- All Implemented Interfaces:
FlowableSubscriber<T>, Flow.Subscriber<T>, Flow.Subscription
public class TestSubscriber<T>
extends BaseTestConsumer<T, TestSubscriber<T>>
implements FlowableSubscriber<T>, Flow.Subscription
A
Flow.Subscriber implementation that records events and allows making assertions about them.
You can override the onSubscribe(Subscription), onNext(Object), onError(Throwable) and
onComplete() methods but not the others (this is by design).
When calling the default request method, you are requesting on behalf of the
wrapped actual Flow.Subscriber if any.
-
Field Summary
Fields inherited from class BaseTestConsumer
checkSubscriptionOnce, completions, done, errors, lastThread, tag, timeout, valuesModifier and TypeFieldDescriptionprotected booleanprotected longThe number of completions.protected final CountDownLatchThe latch that indicates an onError or onComplete has been called.The list of errors received.protected ThreadThe last thread seen by the observer.protected CharSequenceThe optional tag associated with this test consumer.protected booleanIndicates that one of theawaitXmethod has timed out.The list of values received. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a non-forwardingTestSubscriberwith an initial request value ofLong.MAX_VALUE.TestSubscriber(long initialRequest) Constructs a non-forwardingTestSubscriberwith the specified initial request value.TestSubscriber(@NonNull Flow.Subscriber<? super T> downstream) Constructs a forwardingTestSubscriberbut leaves the requesting to the wrappedFlow.Subscriber.TestSubscriber(@NonNull Flow.Subscriber<? super T> actual, long initialRequest) Constructs a forwardingTestSubscriberwith the specified initial request amount and an actualFlow.Subscriberto forward events to. -
Method Summary
Modifier and TypeMethodDescriptionprotected final TestSubscriber<T> Assert that theonSubscribe(Subscription)method was called exactly once.final voidcancel()static <T> @NonNull TestSubscriber<T> create()Creates aTestSubscriberwithLong.MAX_VALUEinitial request amount.static <T> @NonNull TestSubscriber<T> create(long initialRequested) Creates aTestSubscriberwith the given initial request amount.static <T> TestSubscriber<T> create(@NonNull Flow.Subscriber<? super T> delegate) Constructs a forwardingTestSubscriber.protected final voiddispose()Cancel/dispose this test consumer.final booleanfinal booleanReturns true if thisTestSubscriberhas been cancelled.protected final booleanReturns true if this test consumer was cancelled/disposed.voidvoidvoidprotected voidonStart()Called after the onSubscribe is called and handled.voidImplementors of this method should make sure everything that needs to be visible inFlow.Subscriber.onNext(Object)is established before callingFlow.Subscription.request(long).final voidrequest(long n) final TestSubscriber<T> requestMore(long n) Callsrequest(long)and returns this.Methods inherited from class BaseTestConsumer
assertComplete, assertEmpty, assertError, assertError, assertError, assertFailure, assertNoErrors, assertNotComplete, assertNoValues, assertResult, assertValue, assertValue, assertValueAt, assertValueAt, assertValueCount, assertValues, assertValueSequence, assertValuesOnly, await, await, awaitCount, awaitDone, fail, valueAndClass, values, withTagModifier and TypeMethodDescriptionfinal TestSubscriber<T> Assert that thisTestObserver/TestSubscriberreceived exactly oneonCompleteevent.final TestSubscriber<T> Assert that theTestObserver/TestSubscriberhas received aDisposable/SubscriptionviaonSubscribebut no other events.final TestSubscriber<T> assertError(@NonNull Predicate<Throwable> errorPredicate) Asserts that thisTestObserver/TestSubscriberreceived exactly oneonErrorevent for which the provided predicate returnstrue.final TestSubscriber<T> assertError(@NonNull Class<? extends Throwable> errorClass) Asserts that thisTestObserver/TestSubscriberreceived exactly oneonErrorevent which is an instance of the specifiederrorClassClass.final TestSubscriber<T> assertError(@NonNull Throwable error) Assert that thisTestObserver/TestSubscriberreceived exactly the specifiedonErrorevent value.final TestSubscriber<T> assertFailure(@NonNull Class<? extends Throwable> error, T... values) Assert that the upstream signaled the specified values in order and then failed with a specific class or subclass ofThrowable.final TestSubscriber<T> Assert that thisTestObserver/TestSubscriberhas not received anonErrorevent.final TestSubscriber<T> Assert that thisTestObserver/TestSubscriberhas not received anonCompleteevent.final TestSubscriber<T> Assert that thisTestObserver/TestSubscriberhas not received anyonNextevents.final TestSubscriber<T> assertResult(T... values) Assert that the upstream signaled the specified values in order and completed normally.final TestSubscriber<T> assertValue(@NonNull Predicate<T> valuePredicate) Asserts that thisTestObserver/TestSubscriberreceived exactly oneonNextvalue for which the provided predicate returnstrue.final TestSubscriber<T> assertValue(T value) Assert that thisTestObserver/TestSubscriberreceived exactly oneonNextvalue which is equal to the given value with respect toObjects.equals(Object, Object).final TestSubscriber<T> assertValueAt(int index, @NonNull Predicate<T> valuePredicate) Asserts that thisTestObserver/TestSubscriberreceived anonNextvalue at the given index for the provided predicate returnstrue.final TestSubscriber<T> assertValueAt(int index, T value) Asserts that thisTestObserver/TestSubscriberreceived anonNextvalue at the given index which is equal to the given value with respect tonull-safeObjects.equals(Object, Object).final TestSubscriber<T> assertValueCount(int count) Assert that thisTestObserver/TestSubscriberreceived the specified numberonNextevents.final TestSubscriber<T> assertValues(T... values) Assert that theTestObserver/TestSubscriberreceived only the specified values in the specified order.final TestSubscriber<T> assertValueSequence(@NonNull Iterable<? extends T> sequence) Assert that theTestObserver/TestSubscriberreceived only the specified sequence of values in the same order.final TestSubscriber<T> assertValuesOnly(T... values) Assert that theTestObserver/TestSubscriberreceived only the specified values in the specified order without terminating.final TestSubscriber<T> await()Awaits until thisTestObserver/TestSubscriberreceives anonErrororonCompleteevents.final booleanAwaits the specified amount of time or until thisTestObserver/TestSubscriberreceives anonErrororonCompleteevents, whichever happens first.final TestSubscriber<T> awaitCount(int atLeast) Await until theTestObserver/TestSubscriberreceives the given number of items or terminates by sleeping 10 milliseconds at a time up to 5000 milliseconds of timeout.final TestSubscriber<T> Awaits until the internal latch is counted down.protected final @NonNull AssertionErrorFail with the given message and add the sequence of errors as suppressed ones.Appends the class name to a non-nullvalue or returns"null".values()Returns a shared list of receivedonNextvalues or the singleonSuccessvalue.final TestSubscriber<T> withTag(@Nullable CharSequence tag) Set the tag displayed along with an assertion failure's other state information.
-
Constructor Details
-
TestSubscriber
public TestSubscriber()Constructs a non-forwardingTestSubscriberwith an initial request value ofLong.MAX_VALUE. -
TestSubscriber
public TestSubscriber(long initialRequest) Constructs a non-forwardingTestSubscriberwith the specified initial request value.The
TestSubscriberdoesn't validate theinitialRequestamount so one can test sources with invalid values as well.- Parameters:
initialRequest- the initial request amount
-
TestSubscriber
Constructs a forwardingTestSubscriberbut leaves the requesting to the wrappedFlow.Subscriber.- Parameters:
downstream- the actualSubscriberto forward events to
-
TestSubscriber
Constructs a forwardingTestSubscriberwith the specified initial request amount and an actualFlow.Subscriberto forward events to.The
TestSubscriberdoesn't validate the initialRequest value so one can test sources with invalid values as well.- Parameters:
actual- the actualSubscriberto forward events toinitialRequest- the initial request amount
-
-
Method Details
-
create
Creates aTestSubscriberwithLong.MAX_VALUEinitial request amount.- Type Parameters:
T- the value type- Returns:
- the new
TestSubscriberinstance. - See Also:
-
create
Creates aTestSubscriberwith the given initial request amount.- Type Parameters:
T- the value type- Parameters:
initialRequested- the initial requested amount- Returns:
- the new
TestSubscriberinstance.
-
create
Constructs a forwardingTestSubscriber.- Type Parameters:
T- the value type received- Parameters:
delegate- the actualFlow.Subscriberto forward events to- Returns:
- the new TestObserver instance
-
onSubscribe
Description copied from interface:FlowableSubscriberImplementors of this method should make sure everything that needs to be visible inFlow.Subscriber.onNext(Object)is established before callingFlow.Subscription.request(long). In practice this means no initialization should happen after therequest()call and additional behavior is thread safe in respect toonNext.- Specified by:
onSubscribein interfaceFlow.Subscriber<T>- Specified by:
onSubscribein interfaceFlowableSubscriber<T>
-
onStart
protected void onStart()Called after the onSubscribe is called and handled. -
onNext
- Specified by:
onNextin interfaceFlow.Subscriber<T>
-
onError
-
onComplete
public void onComplete()- Specified by:
onCompletein interfaceFlow.Subscriber<T>
-
request
public final void request(long n) - Specified by:
requestin interfaceFlow.Subscription
-
cancel
public final void cancel()- Specified by:
cancelin interfaceFlow.Subscription
-
isCancelled
public final boolean isCancelled()Returns true if thisTestSubscriberhas been cancelled.- Returns:
- true if this
TestSubscriberhas been cancelled
-
dispose
protected final void dispose()Description copied from class:BaseTestConsumerCancel/dispose this test consumer.- Specified by:
disposein classBaseTestConsumer<T, TestSubscriber<T>>
-
isDisposed
protected final boolean isDisposed()Description copied from class:BaseTestConsumerReturns true if this test consumer was cancelled/disposed.- Specified by:
isDisposedin classBaseTestConsumer<T, TestSubscriber<T>>- Returns:
- true if this test consumer was cancelled/disposed.
-
hasSubscription
public final boolean hasSubscription()- Returns:
- true if this
TestSubscriberreceived aFlow.SubscriptionviaonSubscribe(Subscription)
-
assertSubscribed
Assert that theonSubscribe(Subscription)method was called exactly once.- Specified by:
assertSubscribedin classBaseTestConsumer<T, TestSubscriber<T>>- Returns:
- this
-
requestMore
Callsrequest(long)and returns this.History: 2.0.1 - experimental
- Parameters:
n- the request amount- Returns:
- this
- Since:
- 2.1
-