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
