How to perform assertion on all remaining elements in StepVerifier?

时光怂恿深爱的人放手 提交于 2019-12-06 11:01:21

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();

The expected way to do that is to actually use the thenConsumeWhile operator and provide a predicate. If there is any element in the sequence that doesn't match, the StepVerifier will error.

It is not assertion-based though, taking a Predicate rather than a Consumer. That said you could still use an assertion library, any AssertionError should fail the test (you still have to return a bogus predicate result).

(side warning: keep in mind this doesn't work well with infinite sequences)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!