Interface StepVerifier
StepVerifier provides a declarative way of creating a verifiable script for
an async Publisher sequence, by expressing expectations about the events that
will happen upon subscription. The verification must be triggered after the terminal
expectations (completion, error, cancellation) have been declared, by calling one of
the verify() methods.
- Create a
StepVerifieraround aPublisherusingcreate(Publisher)orwithVirtualTime(Supplier<Publisher>)(in which case you should lazily create the publisher inside the providedlambda). - Set up individual value expectations using
expectNext,expectNextMatches(Predicate),assertNext(Consumer),expectNextCount(long)orexpectNextSequence(Iterable). - Trigger subscription actions during the verification using either
thenRequest(long)orthenCancel(). - Finalize the test scenario using a terminal expectation:
expectComplete(),expectError(),expectError(Class),expectErrorMatches(Predicate), orthenCancel(). - Trigger the verification of the resulting
StepVerifieron itsPublisherusing eitherverify()orverify(Duration). (note some of the terminal expectations above have a "verify" prefixed alternative that both declare the expectation and trigger the verification). - If any expectations failed, an
AssertionErrorwill be thrown indicating the failures.
For example:
StepVerifier.create(Flux.just("foo", "bar"))
.expectNext("foo")
.expectNext("bar")
.expectComplete()
.verify();
- Author:
- Arjen Poutsma, Stephane Maldini, Simon Baslé
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceExposes post-verification state assertions.static interfaceAllow to set expectations about theContextpropagated during theSubscriptionphase.static interfaceDefine a builder for explicitly expecting an initializingSubscriptionas first signal.static interfaceDefine a builder for terminal states.static interfaceDefine a builder for expecting main sequence individual signals. -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> StepVerifier.FirstStep<T>Prepare a newStepVerifierin an uncontrolled environment:StepVerifier.Step.thenAwait()will block in real time.static <T> StepVerifier.FirstStep<T>Prepare a newStepVerifierin an uncontrolled environment:StepVerifier.Step.thenAwait()will block in real time.static <T> StepVerifier.FirstStep<T>create(Publisher<? extends T> publisher, StepVerifierOptions options) Prepare a newStepVerifierin an uncontrolled environment:StepVerifier.Step.thenAwait()will block in real time.log()Activate debug logging of a description of the test scenario, as well as some details about certain verification steps.static voidReset theverify()timeout to the "unlimited" default.static voidsetDefaultTimeout(@Nullable Duration timeout) Set theverify()timeout for allStepVerifiercreated through the factory methods (create(Publisher),withVirtualTime(Supplier), etc.).verify()Verify the signals received by this subscriber.Verify the signals received by this subscriber.Trigger the subscription and prepare for verifications but doesn't block.Verifiesthe signals received by this subscriber, then exposes variousassertion methodson the final state.verifyThenAssertThat(Duration duration) Verifiesthe signals received by this subscriber, then exposes variousassertion methodson the final state.static <T> StepVerifier.FirstStep<T>withVirtualTime(Supplier<? extends Publisher<? extends T>> scenarioSupplier) Prepare a newStepVerifierin a controlled environment usingVirtualTimeSchedulerto manipulate a virtual clock viaStepVerifier.Step.thenAwait().static <T> StepVerifier.FirstStep<T>withVirtualTime(Supplier<? extends Publisher<? extends T>> scenarioSupplier, long n) Prepare a newStepVerifierin a controlled environment usingVirtualTimeSchedulerto manipulate a virtual clock viaStepVerifier.Step.thenAwait().static <T> StepVerifier.FirstStep<T>withVirtualTime(Supplier<? extends Publisher<? extends T>> scenarioSupplier, Supplier<? extends VirtualTimeScheduler> vtsLookup, long n) Prepare a newStepVerifierin a controlled environment using a user-providedVirtualTimeSchedulerto manipulate a virtual clock viaStepVerifier.Step.thenAwait().static <T> StepVerifier.FirstStep<T>withVirtualTime(Supplier<? extends Publisher<? extends T>> scenarioSupplier, StepVerifierOptions options) Prepare a newStepVerifierin a controlled environment using a user-providedVirtualTimeSchedulerto manipulate a virtual clock viaStepVerifier.Step.thenAwait().
-
Field Details
-
DEFAULT_VERIFY_TIMEOUT
Default verification timeout (seeverify()) is "no timeout".
-
-
Method Details
-
setDefaultTimeout
Set theverify()timeout for allStepVerifiercreated through the factory methods (create(Publisher),withVirtualTime(Supplier), etc.).This affects ALL such verifiers created after this call, until a call to either this method or
resetDefaultTimeout().- Parameters:
timeout- the timeout to use forverify()calls on allStepVerifiercreated through the factory methods after this call. null is interpreted as a call toresetDefaultTimeout().
-
resetDefaultTimeout
static void resetDefaultTimeout()Reset theverify()timeout to the "unlimited" default.This affects ALL such verifiers created after this call, until a call to
setDefaultTimeout(Duration). -
create
Prepare a newStepVerifierin an uncontrolled environment:StepVerifier.Step.thenAwait()will block in real time. Eachverify()will fully (re)play the scenario.- Parameters:
publisher- the publisher to subscribe to and verify- Returns:
- a builder for expectation declaration and ultimately verification.
-
create
Prepare a newStepVerifierin an uncontrolled environment:StepVerifier.Step.thenAwait()will block in real time. Eachverify()will fully (re)play the scenario. The verification will request a specified amount of values.- Parameters:
publisher- the publisher to subscribe to and verifyn- the amount of items to request- Returns:
- a builder for expectation declaration and ultimately verification.
-
create
static <T> StepVerifier.FirstStep<T> create(Publisher<? extends T> publisher, StepVerifierOptions options) Prepare a newStepVerifierin an uncontrolled environment:StepVerifier.Step.thenAwait()will block in real time. Eachverify()will fully (re)play the scenario. The verification will request a specified amount of values according to theoptionspassed.- Parameters:
publisher- the publisher to subscribe tooptions- the options for the verification- Returns:
- a builder for expectation declaration and ultimately verification.
-
withVirtualTime
static <T> StepVerifier.FirstStep<T> withVirtualTime(Supplier<? extends Publisher<? extends T>> scenarioSupplier) Prepare a newStepVerifierin a controlled environment usingVirtualTimeSchedulerto manipulate a virtual clock viaStepVerifier.Step.thenAwait(). The scheduler is injected into allSchedulersfactories, which means that any operator created within the lambda without a specific scheduler will use virtual time. Eachverify()will fully (re)play the scenario. The verification will request an unbounded amount of values.Note that virtual time,
StepVerifier.Step.thenAwait(Duration)sources that are subscribed on a differentScheduler(eg. a source that is initialized outside of the lambda with a dedicated Scheduler) and delays introduced within the data path (eg. an interval in a flatMap) are not always compatible, as this can perform the clock move BEFORE the interval schedules itself, resulting in the interval never playing out.- Type Parameters:
T- the type of the subscriber- Parameters:
scenarioSupplier- a mandatory supplier of thePublisherto subscribe to and verify. In order for operators to use virtual time, they must be invoked from within the lambda.- Returns:
- a builder for expectation declaration and ultimately verification.
-
withVirtualTime
static <T> StepVerifier.FirstStep<T> withVirtualTime(Supplier<? extends Publisher<? extends T>> scenarioSupplier, long n) Prepare a newStepVerifierin a controlled environment usingVirtualTimeSchedulerto manipulate a virtual clock viaStepVerifier.Step.thenAwait(). The scheduler is injected into allSchedulersfactories, which means that any operator created within the lambda without a specific scheduler will use virtual time. Eachverify()will fully (re)play the scenario. The verification will request a specified amount of values.Note that virtual time,
StepVerifier.Step.thenAwait(Duration)sources that are subscribed on a differentScheduler(eg. a source that is initialized outside of the lambda with a dedicated Scheduler) and delays introduced within the data path (eg. an interval in a flatMap) are not always compatible, as this can perform the clock move BEFORE the interval schedules itself, resulting in the interval never playing out.- Type Parameters:
T- the type of the subscriber- Parameters:
scenarioSupplier- a mandatory supplier of thePublisherto subscribe to and verify. In order for operators to use virtual time, they must be invoked from within the lambda.n- the amount of items to request (must be >= 0)- Returns:
- a builder for expectation declaration and ultimately verification.
-
withVirtualTime
static <T> StepVerifier.FirstStep<T> withVirtualTime(Supplier<? extends Publisher<? extends T>> scenarioSupplier, Supplier<? extends VirtualTimeScheduler> vtsLookup, long n) Prepare a newStepVerifierin a controlled environment using a user-providedVirtualTimeSchedulerto manipulate a virtual clock viaStepVerifier.Step.thenAwait(). The scheduler is injected into allSchedulersfactories, which means that any operator created within the lambda without a specific scheduler will use virtual time. Eachverify()will fully (re)play the scenario. The verification will request a specified amount of values.Note that virtual time,
StepVerifier.Step.thenAwait(Duration)sources that are subscribed on a differentScheduler(eg. a source that is initialized outside of the lambda with a dedicated Scheduler) and delays introduced within the data path (eg. an interval in a flatMap) are not always compatible, as this can perform the clock move BEFORE the interval schedules itself, resulting in the interval never playing out.- Type Parameters:
T- the type of the subscriber- Parameters:
scenarioSupplier- a mandatory supplier of thePublisherto subscribe to and verify. In order for operators to use virtual time, they must be invoked from within the lambda.vtsLookup- the supplier of theVirtualTimeSchedulerto inject and manipulate during verification.n- the amount of items to request (must be >= 0)- Returns:
- a builder for expectation declaration and ultimately verification.
-
withVirtualTime
static <T> StepVerifier.FirstStep<T> withVirtualTime(Supplier<? extends Publisher<? extends T>> scenarioSupplier, StepVerifierOptions options) Prepare a newStepVerifierin a controlled environment using a user-providedVirtualTimeSchedulerto manipulate a virtual clock viaStepVerifier.Step.thenAwait(). The scheduler is injected into allSchedulersfactories, which means that any operator created within the lambda without a specific scheduler will use virtual time. Eachverify()will fully (re)play the scenario. The verification will request a specified amount of values according to the providedoptions.If no
VirtualTimeSchedulerSupplieris set in the options, this method will make acopyof said options and set up the default supplier (like the one inwithVirtualTime(Supplier)).Note that virtual time,
StepVerifier.Step.thenAwait(Duration)sources that are subscribed on a differentScheduler(eg. a source that is initialized outside of the lambda with a dedicated Scheduler) and delays introduced within the data path (eg. an interval in a flatMap) are not always compatible, as this can perform the clock move BEFORE the interval schedules itself, resulting in the interval never playing out.- Type Parameters:
T- the type of the subscriber- Parameters:
scenarioSupplier- a mandatory supplier of thePublisherto subscribe to and verify. In order for operators to use virtual time, they must be invoked from within the lambda.options- the verification options, including the supplier of theVirtualTimeSchedulerto inject and manipulate during verification (see note above in case options doesn't define such a supplier)- Returns:
- a builder for expectation declaration and ultimately verification.
-
log
StepVerifier log()Activate debug logging of a description of the test scenario, as well as some details about certain verification steps.- Returns:
- the verifier for final
verify()call
-
verifyLater
StepVerifier verifyLater()Trigger the subscription and prepare for verifications but doesn't block. Calling one of theverify()methods afterwards will block until the sequence is validated and throw if assertions fail.Calling this method more than once in a row should be a NO-OP, returning the same instance as the first call.
- Returns:
- a
StepVerifierthat is in progress but on which one can chose to block later.
-
verify
Verify the signals received by this subscriber. Unless a default timeout has been set before construction of theStepVerifierviasetDefaultTimeout(Duration), this method will block until the stream has been terminated (either throughSubscriber.onComplete(),Subscriber.onError(Throwable)orSubscription.cancel()). Depending on the declared expectations and actions, notably in case of undersized manual requests, such a verification could also block indefinitely.- Returns:
- the actual
Durationthe verification took. - Throws:
AssertionError- in case of expectation failures- See Also:
-
verify
Verify the signals received by this subscriber. This method will block for up to the given duration or until the stream has been terminated (either throughSubscriber.onComplete(),Subscriber.onError(Throwable)orSubscription.cancel()). UseDuration.ZEROfor an unlimited wait for termination.- Parameters:
duration- the maximum duration to wait for the sequence to terminate, orDuration.ZEROfor unlimited wait.- Returns:
- the actual
Durationthe verification took. - Throws:
AssertionError- in case of expectation failures, or when the verification times out
-
verifyThenAssertThat
StepVerifier.Assertions verifyThenAssertThat()Verifiesthe signals received by this subscriber, then exposes variousassertion methodson the final state.Note that like
verify(), this method will block until the stream has been terminated (either throughSubscriber.onComplete(),Subscriber.onError(Throwable)orSubscription.cancel()). Depending on the declared expectations and actions, notably in case of undersized manual requests, such a verification could also block indefinitely. UsesetDefaultTimeout(Duration)to globally add a timeout on verify()-derived methods.- Returns:
- the actual
Durationthe verification took. - Throws:
AssertionError- in case of expectation failures
-
verifyThenAssertThat
Verifiesthe signals received by this subscriber, then exposes variousassertion methodson the final state.Note that like
verify(), this method will block until the stream has been terminated (either throughSubscriber.onComplete(),Subscriber.onError(Throwable)orSubscription.cancel()). Depending on the declared expectations and actions, notably in case of undersized manual requests, such a verification could also block indefinitely. As a consequence you can use theDurationdurationparameter to set a timeout.- Parameters:
duration- the maximum duration to wait for the sequence to terminate, orDuration.ZEROfor unlimited wait.- Returns:
StepVerifier.Assertionsfor chaining post-verification state assertions
-