问题描述
StepVerifier 有一个 assertNext
方法,允许对下一个元素的值执行断言.
StepVerifier has an assertNext
method that allows performing an assertion on a value of next element.
StepVerifier.create(dataFlux)
.assertNext(v -> checkValue(v)
.verifyComplete();
对每个剩余元素执行断言的好方法是什么(例如检查每个元素是否为正)?我希望像 assertEveryNextElement
方法.
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.
推荐答案
我第一次尝试对通量的所有元素执行断言是
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é 建议在 thenConsume 时使用,它重新抛出 AssertionErrors.
UPD Simon Baslé suggested to use just thenConsumeWhile, it rethrows AssertionErrors.
StepVerifier.create(dataFlux)
.thenConsumeWhile(v -> {
assertThat(v).equalsTo(expected);
return true;
})
.verifyComplete();
使用 StepVerifier 执行此任务的更规范的方法是:
And more canonical way to use StepVerifier for this task would be:
StepVerifier.create(dataFlux)
.thenConsumeWhile(v -> return expected.equals(v))
.verifyComplete();
这篇关于如何对 StepVerifier 中的所有剩余元素执行断言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!