Class BaseTestConsumer<T,U extends BaseTestConsumer<T,U>>

java.lang.Object
io.reactivex.rxjava3.observers.BaseTestConsumer<T,U>
Type Parameters:
T - the value type consumed
U - the subclass of this BaseTestConsumer
Direct Known Subclasses:
TestObserver, TestSubscriber

public abstract class BaseTestConsumer<T,U extends BaseTestConsumer<T,U>> extends Object
Base class with shared infrastructure to support TestSubscriber and TestObserver.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected boolean
     
    protected long
    The number of completions.
    protected final CountDownLatch
    The latch that indicates an onError or onComplete has been called.
    protected final List<Throwable>
    The list of errors received.
    protected Thread
    The last thread seen by the observer.
    protected CharSequence
    The optional tag associated with this test consumer.
    protected boolean
    Indicates that one of the awaitX method has timed out.
    protected final List<T>
    The list of values received.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a BaseTestConsumer with CountDownLatch set to 1.
  • Method Summary

    Modifier and Type
    Method
    Description
    final U
    Assert that this TestObserver/TestSubscriber received exactly one onComplete event.
    final U
    Assert that the TestObserver/TestSubscriber has received a Disposable/Subscription via onSubscribe but no other events.
    final U
    Asserts that this TestObserver/TestSubscriber received exactly one onError event for which the provided predicate returns true.
    private U
    assertError(@NonNull Predicate<Throwable> errorPredicate, boolean exact)
     
    final U
    assertError(@NonNull Class<? extends Throwable> errorClass)
    Asserts that this TestObserver/TestSubscriber received exactly one onError event which is an instance of the specified errorClass Class.
    final U
    Assert that this TestObserver/TestSubscriber received exactly the specified onError event value.
    final U
    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 of Throwable.
    final U
    Assert that this TestObserver/TestSubscriber has not received an onError event.
    final U
    Assert that this TestObserver/TestSubscriber has not received an onComplete event.
    final U
    Assert that this TestObserver/TestSubscriber has not received any onNext events.
    final U
    assertResult(T... values)
    Assert that the upstream signaled the specified values in order and completed normally.
    protected abstract U
    Assert that the onSubscribe method was called exactly once.
    final U
    assertValue(@NonNull Predicate<T> valuePredicate)
    Asserts that this TestObserver/TestSubscriber received exactly one onNext value for which the provided predicate returns true.
    final U
    assertValue(T value)
    Assert that this TestObserver/TestSubscriber received exactly one onNext value which is equal to the given value with respect to Objects.equals(Object, Object).
    final U
    assertValueAt(int index, @NonNull Predicate<T> valuePredicate)
    Asserts that this TestObserver/TestSubscriber received an onNext value at the given index for the provided predicate returns true.
    final U
    assertValueAt(int index, T value)
    Asserts that this TestObserver/TestSubscriber received an onNext value at the given index which is equal to the given value with respect to null-safe Objects.equals(Object, Object).
    final U
    assertValueCount(int count)
    Assert that this TestObserver/TestSubscriber received the specified number onNext events.
    final U
    assertValues(T... values)
    Assert that the TestObserver/TestSubscriber received only the specified values in the specified order.
    final U
    assertValueSequence(@NonNull Iterable<? extends T> sequence)
    Assert that the TestObserver/TestSubscriber received only the specified sequence of values in the same order.
    final U
    assertValuesOnly(T... values)
    Assert that the TestObserver/TestSubscriber received only the specified values in the specified order without terminating.
    final U
    Awaits until this TestObserver/TestSubscriber receives an onError or onComplete events.
    final boolean
    await(long time, @NonNull TimeUnit unit)
    Awaits the specified amount of time or until this TestObserver/TestSubscriber receives an onError or onComplete events, whichever happens first.
    final U
    awaitCount(int atLeast)
    Await until the TestObserver/TestSubscriber receives the given number of items or terminates by sleeping 10 milliseconds at a time up to 5000 milliseconds of timeout.
    final U
    awaitDone(long time, @NonNull TimeUnit unit)
    Awaits until the internal latch is counted down.
    protected abstract void
    Cancel/dispose this test consumer.
    protected final @NonNull AssertionError
    fail(@NonNull String message)
    Fail with the given message and add the sequence of errors as suppressed ones.
    protected abstract boolean
    Returns true if this test consumer was cancelled/disposed.
    Appends the class name to a non-null value or returns "null".
    final @NonNull List<T>
    Returns a shared list of received onNext values or the single onSuccess value.
    final U
    Set the tag displayed along with an assertion failure's other state information.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • done

      protected final CountDownLatch done
      The latch that indicates an onError or onComplete has been called.
    • values

      protected final List<T> values
      The list of values received.
    • errors

      protected final List<Throwable> errors
      The list of errors received.
    • completions

      protected long completions
      The number of completions.
    • lastThread

      protected Thread lastThread
      The last thread seen by the observer.
    • checkSubscriptionOnce

      protected boolean checkSubscriptionOnce
    • tag

      protected CharSequence tag
      The optional tag associated with this test consumer.
      Since:
      2.0.7
    • timeout

      protected boolean timeout
      Indicates that one of the awaitX method has timed out.
      Since:
      2.0.7
  • Constructor Details

    • BaseTestConsumer

      public BaseTestConsumer()
      Constructs a BaseTestConsumer with CountDownLatch set to 1.
  • Method Details

    • values

      @NonNull public final @NonNull List<T> values()
      Returns a shared list of received onNext values or the single onSuccess value.

      Note that accessing the items via certain methods of the List interface while the upstream is still actively emitting more items may result in a ConcurrentModificationException.

      The List.size() method will return the number of items already received by this TestObserver/TestSubscriber in a thread-safe manner that can be read via List.get(int)) method (index range of 0 to List.size() - 1).

      A view of the returned List can be created via List.subList(int, int) by using the bounds 0 (inclusive) to List.size() (exclusive) which, when accessed in a read-only fashion, should be also thread-safe and not throw any ConcurrentModificationException.

      Returns:
      a list of received onNext values
    • fail

      @NonNull protected final @NonNull AssertionError fail(@NonNull @NonNull String message)
      Fail with the given message and add the sequence of errors as suppressed ones.

      Note this is deliberately the only fail method. Most of the times an assertion would fail but it is possible it was due to an exception somewhere. This construct will capture those potential errors and report it along with the original failure.

      Parameters:
      message - the message to use
      Returns:
      AssertionError the prepared AssertionError instance
    • await

      @NonNull public final U await() throws InterruptedException
      Awaits until this TestObserver/TestSubscriber receives an onError or onComplete events.
      Returns:
      this
      Throws:
      InterruptedException - if the current thread is interrupted while waiting
    • await

      public final boolean await(long time, @NonNull @NonNull TimeUnit unit) throws InterruptedException
      Awaits the specified amount of time or until this TestObserver/TestSubscriber receives an onError or onComplete events, whichever happens first.
      Parameters:
      time - the waiting time
      unit - the time unit of the waiting time
      Returns:
      true if the TestObserver/TestSubscriber terminated, false if timeout happened
      Throws:
      InterruptedException - if the current thread is interrupted while waiting
    • assertComplete

      @NonNull public final U assertComplete()
      Assert that this TestObserver/TestSubscriber received exactly one onComplete event.
      Returns:
      this
    • assertNotComplete

      @NonNull public final U assertNotComplete()
      Assert that this TestObserver/TestSubscriber has not received an onComplete event.
      Returns:
      this
    • assertNoErrors

      @NonNull public final U assertNoErrors()
      Assert that this TestObserver/TestSubscriber has not received an onError event.
      Returns:
      this
    • assertError

      @NonNull public final U assertError(@NonNull @NonNull Throwable error)
      Assert that this TestObserver/TestSubscriber received exactly the specified onError event value.

      The comparison is performed via Objects.equals(Object, Object); 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.

      Parameters:
      error - the error to check
      Returns:
      this
      See Also:
    • assertError

      @NonNull public final U assertError(@NonNull @NonNull Class<? extends Throwable> errorClass)
      Asserts that this TestObserver/TestSubscriber received exactly one onError event which is an instance of the specified errorClass Class.
      Parameters:
      errorClass - the error Class to expect
      Returns:
      this
    • assertError

      @NonNull public final U assertError(@NonNull @NonNull Predicate<Throwable> errorPredicate)
      Asserts that this TestObserver/TestSubscriber received exactly one onError event for which the provided predicate returns true.
      Parameters:
      errorPredicate - the predicate that receives the error Throwable and should return true for expected errors.
      Returns:
      this
    • assertError

      @NonNull private U assertError(@NonNull @NonNull Predicate<Throwable> errorPredicate, boolean exact)
    • assertValue

      @NonNull public final U assertValue(@NonNull T value)
      Assert that this TestObserver/TestSubscriber received exactly one onNext value which is equal to the given value with respect to Objects.equals(Object, Object).
      Parameters:
      value - the value to expect
      Returns:
      this
    • assertValue

      @NonNull public final U assertValue(@NonNull @NonNull Predicate<T> valuePredicate)
      Asserts that this TestObserver/TestSubscriber received exactly one onNext value for which the provided predicate returns true.
      Parameters:
      valuePredicate - the predicate that receives the onNext value and should return true for the expected value.
      Returns:
      this
    • assertValueAt

      @NonNull public final U assertValueAt(int index, @NonNull T value)
      Asserts that this TestObserver/TestSubscriber received an onNext value at the given index which is equal to the given value with respect to null-safe Objects.equals(Object, Object).

      History: 2.1.3 - experimental

      Parameters:
      index - the position to assert on
      value - the value to expect
      Returns:
      this
      Since:
      2.2
    • assertValueAt

      @NonNull public final U assertValueAt(int index, @NonNull @NonNull Predicate<T> valuePredicate)
      Asserts that this TestObserver/TestSubscriber received an onNext value at the given index for the provided predicate returns true.
      Parameters:
      index - the position to assert on
      valuePredicate - the predicate that receives the onNext value and should return true for the expected value.
      Returns:
      this
    • valueAndClass

      @NonNull public static @NonNull String valueAndClass(@Nullable @Nullable Object o)
      Appends the class name to a non-null value or returns "null".
      Parameters:
      o - the object
      Returns:
      the string representation
    • assertValueCount

      @NonNull public final U assertValueCount(int count)
      Assert that this TestObserver/TestSubscriber received the specified number onNext events.
      Parameters:
      count - the expected number of onNext events
      Returns:
      this
    • assertNoValues

      @NonNull public final U assertNoValues()
      Assert that this TestObserver/TestSubscriber has not received any onNext events.
      Returns:
      this
    • assertValues

      @SafeVarargs @NonNull public final U assertValues(@NonNull T... values)
      Assert that the TestObserver/TestSubscriber received only the specified values in the specified order.
      Parameters:
      values - the values expected
      Returns:
      this
    • assertValuesOnly

      @SafeVarargs @NonNull public final U assertValuesOnly(@NonNull T... values)
      Assert that the TestObserver/TestSubscriber received only the specified values in the specified order without terminating.

      History: 2.1.4 - experimental

      Parameters:
      values - the values expected
      Returns:
      this
      Since:
      2.2
    • assertValueSequence

      @NonNull public final U assertValueSequence(@NonNull @NonNull Iterable<? extends T> sequence)
      Assert that the TestObserver/TestSubscriber received only the specified sequence of values in the same order.
      Parameters:
      sequence - the sequence of expected values in order
      Returns:
      this
    • assertSubscribed

      @NonNull protected abstract U assertSubscribed()
      Assert that the onSubscribe method was called exactly once.
      Returns:
      this
    • assertResult

      @SafeVarargs @NonNull public final U assertResult(@NonNull T... values)
      Assert that the upstream signaled the specified values in order and completed normally.
      Parameters:
      values - the expected values, asserted in order
      Returns:
      this
      See Also:
    • assertFailure

      @SafeVarargs @NonNull public final U assertFailure(@NonNull @NonNull Class<? extends Throwable> error, @NonNull T... values)
      Assert that the upstream signaled the specified values in order and then failed with a specific class or subclass of Throwable.
      Parameters:
      error - the expected exception (parent) Class
      values - the expected values, asserted in order
      Returns:
      this
    • awaitDone

      @NonNull public final U awaitDone(long time, @NonNull @NonNull TimeUnit unit)
      Awaits until the internal latch is counted down.

      If the wait times out or gets interrupted, the TestObserver/TestSubscriber is cancelled.

      Parameters:
      time - the waiting time
      unit - the time unit of the waiting time
      Returns:
      this
      Throws:
      RuntimeException - wrapping an InterruptedException if the wait is interrupted
    • assertEmpty

      @NonNull public final U assertEmpty()
      Assert that the TestObserver/TestSubscriber has received a Disposable/Subscription via onSubscribe but no other events.
      Returns:
      this
    • withTag

      @NonNull public final U withTag(@Nullable @Nullable CharSequence tag)
      Set the tag displayed along with an assertion failure's other state information.

      History: 2.0.7 - experimental

      Parameters:
      tag - the string to display (null won't print any tag)
      Returns:
      this
      Since:
      2.1
    • awaitCount

      @NonNull public final U awaitCount(int atLeast)
      Await until the TestObserver/TestSubscriber receives the given number of items or terminates by sleeping 10 milliseconds at a time up to 5000 milliseconds of timeout.

      History: 2.0.7 - experimental

      Parameters:
      atLeast - the number of items expected at least
      Returns:
      this
      Since:
      2.1
    • isDisposed

      protected abstract boolean isDisposed()
      Returns true if this test consumer was cancelled/disposed.
      Returns:
      true if this test consumer was cancelled/disposed.
    • dispose

      protected abstract void dispose()
      Cancel/dispose this test consumer.