StepVerifier has an assertNext
method that allows performing an assertion on a value of next element.
StepVerifier.create(dataFlux)
.assertNext(v -> checkValue(v)
.verifyComplete();
What is a good way to perform assertion for every remaining element (e.g. to check that every element is positive)? I'd expect something like assertEveryNextElement
method.
StepVerifier has an assertNext method that allows performing an assertion on a value of next element.
expectNext(T t) Expect the next element received to be equal to the given value.
A 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.
With the StepVerifier API, we can define our expectations of published elements in terms of what elements we expect and what happens when our stream completes.
My first attempt to perform assertion on all elements of a flux was
StepVerifier.create(dataFlux)
.recordWith(ArrayList::new)
.thenConsumeWhile(x -> true) // Predicate on elements
.consumeRecordedWith(matches ->
matches.forEach(v -> checkValue(v)))
.verifyComplete();
UPD Simon Baslé suggested to use just thenConsumeWhile, it rethrows AssertionErrors.
StepVerifier.create(dataFlux)
.thenConsumeWhile(v -> {
assertThat(v).equalsTo(expected);
return true;
})
.verifyComplete();
And more canonical way to use StepVerifier for this task would be:
StepVerifier.create(dataFlux)
.thenConsumeWhile(v -> return expected.equals(v))
.verifyComplete();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With