59

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?

Mark
  • 4,970
  • 5
  • 42
  • 66

2 Answers2

84

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.

Simon Baslé
  • 27,105
  • 5
  • 69
  • 70
18

here it is checked that onNext is not called

 StepVerifier.create(result).expectNextCount(0).verifyComplete()
samibel
  • 634
  • 2
  • 6
  • 20
  • 7
    Please explain the difference to the other, older, upvoted answer. It seems that you recommend the same thing, just without any explanation – Yunnosch Dec 07 '20 at 23:05