如何使用 Reactor 的 StepVerifier 来验证 Mono 是否为空?

How do I use Reactor's StepVerifier to verify a Mono is empty?

我正在使用 StepVerifier 来测试值:

@Test
public void testStuff() {
    Thing thing = new Thing();
    Mono<Thing> result = Mono.just(thing);
    StepVerifier.create(result).consumeNextWith(r -> {
        assertEquals(thing, r);
    }).verifyComplete();
}

我现在想做的是测试 Mono 中是否缺少某个项目。像这样:

@Test
public void testNoStuff() {
    Mono<Thing> result = Mono.empty();
    StepVerifier.create(result)... // what goes here?
}

我想测试一下 Mono 实际上是空的。我该怎么做?

只需使用verifyComplete()。如果 Mono 发出任何数据,它将使 stepverifier 失败,因为它此时不期望 onNext 信号。

这里检查没有调用onNext

 StepVerifier.create(result).expectNextCount(0).verifyComplete()