Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform assertion on all remaining elements in StepVerifier?

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.

like image 440
Dmitriusan Avatar asked Jun 26 '17 15:06

Dmitriusan


People also ask

What does assertNext do?

StepVerifier has an assertNext method that allows performing an assertion on a value of next element.

What is expectNext?

expectNext(T t) Expect the next element received to be equal to the given value.

What is step verifier?

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.

What is the use of StepVerifier?

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.


1 Answers

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();
like image 160
Dmitriusan Avatar answered Oct 06 '22 22:10

Dmitriusan