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

2

63

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?

Howl answered 18/7, 2018 at 23:58 Comment(0)
O
90

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.

Overestimate answered 23/7, 2018 at 13:1 Comment(0)
M
20

here it is checked that onNext is not called

 StepVerifier.create(result).expectNextCount(0).verifyComplete()
Marshamarshal answered 7/12, 2020 at 15:48 Comment(2)
Please explain the difference to the other, older, upvoted answer. It seems that you recommend the same thing, just without any explanationPetersen
expectNextCount will be keeping track of how many elements are pending to iterate. If there is no elements in the flux/mono, it will match with 0 & the test case will passEscobar

© 2022 - 2024 — McMap. All rights reserved.