T
- the value typepublic class TestSubscriber<T> extends java.lang.Object implements org.reactivestreams.Subscriber<T>, org.reactivestreams.Subscription, Disposable
You can override the onSubscribe, onNext, onError, onComplete, request and cancel methods but not the others (this is by design).
The TestSubscriber implements Disposable for convenience where dispose calls cancel.
When calling the default request method, you are requesting on behalf of the wrapped actual subscriber.
Constructor and Description |
---|
TestSubscriber()
Constructs a non-forwarding TestSubscriber with an initial request value of Long.MAX_VALUE.
|
TestSubscriber(long initialRequest)
Constructs a non-forwarding TestSubscriber with the specified initial request value.
|
TestSubscriber(org.reactivestreams.Subscriber<? super T> actual)
Constructs a forwarding TestSubscriber but leaves the requesting to the wrapped subscriber.
|
TestSubscriber(org.reactivestreams.Subscriber<? super T> actual,
long initialRequest)
Constructs a forwarding TestSubscriber with the specified initial request value.
|
Modifier and Type | Method and Description |
---|---|
TestSubscriber<T> |
assertComplete()
Assert that this TestSubscriber received exactly one onComplete event.
|
TestSubscriber<T> |
assertEmpty()
Assert that the TestSubscriber has received a Subscription but no other events.
|
TestSubscriber<T> |
assertError(java.lang.Class<? extends java.lang.Throwable> errorClass)
Asserts that this TestSubscriber received exactly one onError event which is an
instance of the specified errorClass class.
|
TestSubscriber<T> |
assertError(Predicate<java.lang.Throwable> errorPredicate)
Asserts that this TestSubscriber received exactly one onError event for which
the provided predicate returns true.
|
TestSubscriber<T> |
assertError(java.lang.Throwable error)
Assert that this TestSubscriber received exactly the specified onError event value.
|
TestSubscriber<T> |
assertErrorMessage(java.lang.String message)
Assert that there is only a single error with the given message.
|
TestSubscriber<T> |
assertFailure(java.lang.Class<? extends java.lang.Throwable> error,
T... values)
Assert that the upstream signalled the specified values in order
and then failed with a specific class or subclass of Throwable.
|
TestSubscriber<T> |
assertFailure(Predicate<java.lang.Throwable> errorPredicate,
T... values)
Assert that the upstream signalled the specified values in order and then failed
with a Throwable for which the provided predicate returns true.
|
TestSubscriber<T> |
assertFailureAndMessage(java.lang.Class<? extends java.lang.Throwable> error,
java.lang.String message,
T... values)
Assert that the upstream signalled the specified values in order,
then failed with a specific class or subclass of Throwable
and with the given exact error message.
|
TestSubscriber<T> |
assertNoErrors()
Assert that this TestSubscriber has not received any onError event.
|
TestSubscriber<T> |
assertNotComplete()
Assert that this TestSubscriber has not received any onComplete event.
|
TestSubscriber<T> |
assertNotSubscribed()
Assert that the onSubscribe method hasn't been called at all.
|
TestSubscriber<T> |
assertNotTerminated()
Assert that the TestSubscriber has not terminated (i.e., the terminal latch is still non-zero).
|
TestSubscriber<T> |
assertNoValues()
Assert that this TestSubscriber has not received any onNext events.
|
TestSubscriber<T> |
assertOf(Consumer<? super TestSubscriber<T>> check)
Run a check consumer with this TestSubscriber instance.
|
TestSubscriber<T> |
assertResult(T... values)
Assert that the upstream signalled the specified values in order and
completed normally.
|
TestSubscriber<T> |
assertSubscribed()
Assert that the onSubscribe method was called exactly once.
|
TestSubscriber<T> |
assertTerminated()
Assert that the TestSubscriber terminated (i.e., the terminal latch reached zero).
|
TestSubscriber<T> |
assertValue(T value)
Assert that this TestSubscriber received exactly one onNext value which is equal to
the given value with respect to Objects.equals.
|
TestSubscriber<T> |
assertValueCount(int count)
Assert that this TestSubscriber received the specified number onNext events.
|
TestSubscriber<T> |
assertValues(T... values)
Assert that the TestSubscriber received only the specified values in the specified order.
|
TestSubscriber<T> |
assertValueSequence(java.lang.Iterable<? extends T> sequence)
Assert that the TestSubscriber received only the specified sequence of values in the same order.
|
TestSubscriber<T> |
assertValueSet(java.util.Collection<? extends T> expected)
Assert that the TestSubscriber received only the specified values in any order.
|
TestSubscriber<T> |
await()
Awaits until this TestSubscriber receives an onError or onComplete events.
|
boolean |
await(long time,
java.util.concurrent.TimeUnit unit)
Awaits the specified amount of time or until this TestSubscriber
receives an onError or onComplete events, whichever happens first.
|
TestSubscriber<T> |
awaitDone(long time,
java.util.concurrent.TimeUnit unit)
Awaits until the internal latch is counted down for the specified duration.
|
boolean |
awaitTerminalEvent()
Waits until the any terminal event has been received by this TestSubscriber
or returns false if the wait has been interrupted.
|
boolean |
awaitTerminalEvent(long duration,
java.util.concurrent.TimeUnit unit)
Awaits the specified amount of time or until this TestSubscriber
receives an onError or onComplete events, whichever happens first.
|
void |
cancel() |
long |
completions()
Returns the number of times onComplete was called.
|
static <T> TestSubscriber<T> |
create()
Creates a TestSubscriber with Long.MAX_VALUE initial request.
|
static <T> TestSubscriber<T> |
create(long initialRequested)
Creates a TestSubscriber with the given initial request.
|
static <T> TestSubscriber<T> |
create(org.reactivestreams.Subscriber<? super T> delegate)
Constructs a forwarding TestSubscriber.
|
void |
dispose()
Dispose the resource, the operation should be idempotent.
|
int |
errorCount()
Returns the number of onError exceptions received.
|
java.util.List<java.lang.Throwable> |
errors()
Returns a shared list of received onError exceptions.
|
java.util.List<java.util.List<java.lang.Object>> |
getEvents()
Returns a list of 3 other lists: the first inner list contains the plain
values received; the second list contains the potential errors
and the final list contains the potential completions as Notifications.
|
boolean |
hasSubscription()
Returns true if this TestSubscriber received a subscription.
|
boolean |
isCancelled()
Returns true if this TestSubscriber has been cancelled.
|
boolean |
isDisposed()
Returns true if this resource has been disposed.
|
boolean |
isTerminated()
Returns true if TestSubscriber received any onError or onComplete events.
|
java.lang.Thread |
lastThread()
Returns the last thread which called the onXXX methods of this TestSubscriber.
|
void |
onComplete() |
void |
onError(java.lang.Throwable t) |
void |
onNext(T t) |
protected void |
onStart()
Called after the onSubscribe is called and handled.
|
void |
onSubscribe(org.reactivestreams.Subscription s) |
void |
request(long n) |
int |
valueCount()
Returns the number of onNext values received.
|
java.util.List<T> |
values()
Returns a shared list of received onNext values.
|
public TestSubscriber()
public TestSubscriber(long initialRequest)
The TestSubscriber doesn't validate the initialRequest value so one can test sources with invalid values as well.
initialRequest
- the initial request valuepublic TestSubscriber(org.reactivestreams.Subscriber<? super T> actual)
actual
- the actual Subscriber to forward events topublic TestSubscriber(org.reactivestreams.Subscriber<? super T> actual, long initialRequest)
The TestSubscriber doesn't validate the initialRequest value so one can test sources with invalid values as well.
actual
- the actual Subscriber to forward events toinitialRequest
- the initial request valuepublic static <T> TestSubscriber<T> create()
T
- the value typepublic static <T> TestSubscriber<T> create(long initialRequested)
T
- the value typeinitialRequested
- the initial requested amountpublic static <T> TestSubscriber<T> create(org.reactivestreams.Subscriber<? super T> delegate)
T
- the value type receiveddelegate
- the actual Subscriber to forward events topublic void onSubscribe(org.reactivestreams.Subscription s)
onSubscribe
in interface org.reactivestreams.Subscriber<T>
protected void onStart()
public void onError(java.lang.Throwable t)
onError
in interface org.reactivestreams.Subscriber<T>
public void onComplete()
onComplete
in interface org.reactivestreams.Subscriber<T>
public final void request(long n)
request
in interface org.reactivestreams.Subscription
public final void cancel()
cancel
in interface org.reactivestreams.Subscription
public final boolean isCancelled()
public final void dispose()
Disposable
dispose
in interface Disposable
public final boolean isDisposed()
Disposable
isDisposed
in interface Disposable
public final java.lang.Thread lastThread()
public final java.util.List<T> values()
public final java.util.List<java.lang.Throwable> errors()
public final long completions()
public final boolean isTerminated()
public final int valueCount()
public final int errorCount()
public final boolean hasSubscription()
public final TestSubscriber<T> await() throws java.lang.InterruptedException
java.lang.InterruptedException
- if the current thread is interrupted while waitingawaitTerminalEvent()
public final TestSubscriber<T> assertComplete()
public final TestSubscriber<T> assertNotComplete()
public final TestSubscriber<T> assertNoErrors()
public final TestSubscriber<T> assertError(java.lang.Throwable error)
The comparison is performed via Objects.equals(); since most exceptions don't
implement equals(), this assertion may fail. Use the assertError(Class)
overload to test against the class of an error instead of an instance of an error
or assertError(Predicate)
to test with different condition.
error
- the error to checkassertError(Class)
,
assertError(Predicate)
public final TestSubscriber<T> assertError(java.lang.Class<? extends java.lang.Throwable> errorClass)
errorClass
- the error class to expectpublic final TestSubscriber<T> assertError(Predicate<java.lang.Throwable> errorPredicate)
errorPredicate
- the predicate that receives the error Throwable
and should return true for expected errors.public final TestSubscriber<T> assertValue(T value)
value
- the value to expectpublic final TestSubscriber<T> assertValueCount(int count)
count
- the expected number of onNext eventspublic final TestSubscriber<T> assertNoValues()
public final TestSubscriber<T> assertValues(T... values)
values
- the values expectedassertValueSet(Collection)
public final TestSubscriber<T> assertValueSet(java.util.Collection<? extends T> expected)
This helps asserting when the order of the values is not guaranteed, i.e., when merging asynchronous streams.
expected
- the collection of values expected in any orderpublic final TestSubscriber<T> assertValueSequence(java.lang.Iterable<? extends T> sequence)
sequence
- the sequence of expected values in orderpublic final TestSubscriber<T> assertTerminated()
public final TestSubscriber<T> assertNotTerminated()
public final TestSubscriber<T> assertSubscribed()
public final TestSubscriber<T> assertNotSubscribed()
public final boolean awaitTerminalEvent()
public final boolean awaitTerminalEvent(long duration, java.util.concurrent.TimeUnit unit)
duration
- the waiting timeunit
- the time unit of the waiting timepublic final TestSubscriber<T> assertErrorMessage(java.lang.String message)
message
- the message to checkpublic final java.util.List<java.util.List<java.lang.Object>> getEvents()
public final TestSubscriber<T> assertOf(Consumer<? super TestSubscriber<T>> check)
check
- the check consumer to runpublic final TestSubscriber<T> assertResult(T... values)
values
- the expected values, asserted in orderassertFailure(Class, Object...)
,
assertFailure(Predicate, Object...)
,
assertFailureAndMessage(Class, String, Object...)
public final TestSubscriber<T> assertFailure(java.lang.Class<? extends java.lang.Throwable> error, T... values)
error
- the expected exception (parent) classvalues
- the expected values, asserted in orderpublic final TestSubscriber<T> assertFailure(Predicate<java.lang.Throwable> errorPredicate, T... values)
errorPredicate
- the predicate that receives the error Throwable
and should return true for expected errors.values
- the expected values, asserted in orderpublic final TestSubscriber<T> assertFailureAndMessage(java.lang.Class<? extends java.lang.Throwable> error, java.lang.String message, T... values)
error
- the expected exception (parent) classmessage
- the expected failure messagevalues
- the expected values, asserted in orderpublic final boolean await(long time, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
time
- the waiting timeunit
- the time unit of the waiting timejava.lang.InterruptedException
- if the current thread is interrupted while waitingawaitTerminalEvent(long, TimeUnit)
public final TestSubscriber<T> awaitDone(long time, java.util.concurrent.TimeUnit unit)
If the wait times out or gets interrupted, the TestSubscriber is cancelled.
time
- the waiting timeunit
- the time unit of the waiting timejava.lang.RuntimeException
- wrapping an InterruptedException if the wait is interruptedpublic final TestSubscriber<T> assertEmpty()