Interface PublisherProbe<T>

All Known Implementing Classes:
PublisherProbe.DefaultPublisherProbe, TestPublisher

public interface PublisherProbe<T>
A test utility that allow to easily obtain an instrumented Publisher (Mono or Flux) for tests involving control flow. For instance, you might have a Mono.switchIfEmpty(Mono) and you want to make sure that your code branched into the "if empty" case. The contract of this interface does not cover what signals the Publisher emits, although factory methods of(Publisher) and empty() produce probes that do emit signals like a common sequence.

The PublisherProbe acts as a probe capturing subscription, cancellation and request events. Later, it can be used post completion to check if that particular probe was hit.

Even though TestPublisher implements PublisherProbe, prefer creating probes through the static empty() and of(Publisher) methods. This is because the TestPublisher exposes assertions from PublisherProbe but still requires you to 1) use the TestPublisher emit methods and 2) use the TestPublisher.Violation.CLEANUP_ON_TERMINATE in order for these assertions to be usable post-completion...

Author:
Simon Baslé
  • Method Details

    • assertWasNotSubscribed

      default void assertWasNotSubscribed()
      Check that the probe was never subscribed to, or throw an AssertionError.
    • assertWasSubscribed

      default void assertWasSubscribed()
      Check that the probe was subscribed to at least once, or throw an AssertionError.
    • assertWasNotCancelled

      default void assertWasNotCancelled()
      Check that the probe was never cancelled, or throw an AssertionError.
    • assertWasCancelled

      default void assertWasCancelled()
      Check that the probe was cancelled at least once, or throw an AssertionError.
    • assertWasNotRequested

      default void assertWasNotRequested()
      Check that the probe was never requested, or throw an AssertionError.
    • assertWasRequested

      default void assertWasRequested()
      Check that the probe was requested at least once, or throw an AssertionError.
    • mono

      Mono<T> mono()
      Return a Mono version of the probe. Note all calls to mono() and flux() are backed by the same PublisherProbe and as such influence a single state.

      If the probe was created out of a Publisher, the Flux will forward the signals from this publisher (up to one Subscriber.onNext(Object) though). Otherwise it will simply complete.

      Returns:
      a Mono version of the probe.
    • flux

      Flux<T> flux()
      Return a Flux version of the probe. Note all calls to mono() and flux() are backed by the same PublisherProbe and as such influence a single state.

      If the probe was created out of a Publisher, the Flux will forward the signals from this publisher Otherwise it will simply complete.

      Returns:
      a Flux version of the probe.
    • wasSubscribed

      boolean wasSubscribed()
      Returns:
      true if the probe was subscribed to at least once.
    • subscribeCount

      long subscribeCount()
      Returns:
      how many times probe was subscribed
    • wasCancelled

      boolean wasCancelled()
      Returns:
      true if the probe was cancelled to at least once.
    • wasRequested

      boolean wasRequested()
      Returns:
      true if the probe was requested at least once.
    • of

      static <T> PublisherProbe<T> of(Publisher<? extends T> source)
      Create a PublisherProbe out of a Publisher, ensuring that its flux() and mono() versions will propagate signals from this publisher while capturing subscription, cancellation and request events around it.
      Type Parameters:
      T - the type of the source publisher.
      Parameters:
      source - the source publisher to mimic and probe.
      Returns:
      a probe that mimics the publisher.
    • empty

      static <T> PublisherProbe<T> empty()
      Create a PublisherProbe of which flux() and mono() versions will simply complete, capturing subscription, cancellation and request events around them.
      Type Parameters:
      T - the type of the empty probe.
      Returns:
      a probe that mimics an empty publisher.