liveBook · Manning

StepVerifier

As explained in Spring in Action, Sixth Edition

StepVerifier is a Project Reactor testing utility that subscribes to a Publisher and verifies its emitted signals and completion behavior.

StepVerifier tests a Reactor Flux or Mono by subscribing to it and checking what it emits. Unlike printing values with subscribe(), it can assert each expected value and verify that the stream completes. section 11.3.1

Check a sequence of values

Create a verifier for a fruitFlux, call expectNext("Apple") and then expectNext("Orange") to check values in order, and finish with verifyComplete(). The completion check confirms that the expected sequence ends without further values. section 11.3.1

StepVerifier.create(fruitFlux)
    .expectNext("Apple")
    .expectNext("Orange")
    .expectNext("Grape")
    .expectNext("Banana")
    .expectNext("Strawberry")
    .verifyComplete();

The fruit example shows the full assertion chain, from subscribing with create() through each expected value to verifyComplete(). section 11.3.1

The same pattern works for reactive persistence tests. With @DataR2dbcTest, an injected repository can save ingredients and return a Flux for StepVerifier to check; a findBySlug() call returns a Mono whose ingredient can also be checked. Tests of an order aggregate service verify that saved and fetched orders include their child tacos, then call verifyComplete() to ensure each Mono is finished. section 13.1.3 section 13.1.4

Choose the assertion to fit the result. For a sequence with a known order, expectNext() checks values as they arrive. A MongoDB repository’s findAll() does not guarantee document order, so recordWith() can collect its results into a list; consumeRecordedWith() then checks that the expected ingredients are present regardless of order. section 11.3.1 section 13.2.3

How Spring in Action, Sixth Edition covers it

How deep it goes
Shows assertion chains, completion checks, and recorded-result checks, without describing StepVerifier internals.
Examples
Uses a fruit Flux, ingredient repository tests, and order-saving tests to demonstrate assertions.
What it assumes
Introduces subscription through a Flux example, then uses Flux, Mono, and Spring Data repositories in tests.
What you can do after
Apply it via Reactor to verify emitted values and completion, including results whose order is not guaranteed.
How it connects
Connects Flux and Mono subscription to Spring Data R2DBC, reactive MongoDB repositories, and Spring test annotations.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.