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

Mark picture Mark · Jul 19, 2018 · Viewed 9k times · Source

I am using StepVerifier to test values:

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

What I'd like to do now is test for the absence of an item in the Mono. Like this:

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

I want to test that the Mono is in fact empty. How do I do that?

Answer

Simon Basl&#233; picture Simon Baslé · Jul 23, 2018

Simply use verifyComplete(). If the Mono emits any data, it will fail the stepverifier as it doesn't expect an onNext signal at this point.