Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 951
Mark Avatar asked Jul 18 '18 23:07

Mark


People also ask

Do something if Mono is empty?

Default value if mono is empty. If you want to provide a default value when the mono is completed without any data, then use defaultIfEmpty method. For example, the following code tries to fetch the customer data from the database by using the customer id .

What is the use of StepVerifier?

StepVerifier is provided by reactor test dependency that supports the testing of the reactive aspects of the code. It provides a declarative way to expect events that will occur on the subscription of an async publisher and it only gets triggered after terminal methods have been invoked i.e verify().

What is StepVerifier in Java?

A StepVerifier provides a declarative way of creating a verifiable script for an async Publisher sequence, by expressing expectations about the events that will happen upon subscription.

What is mono just?

The Mono. just method is the simplest method for Mono generation. It takes a single value and generates a finite Mono stream from it. A completion event is published after publishing the specified value: Mono.


Video Answer


2 Answers

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.

like image 67
Simon Baslé Avatar answered Oct 02 '22 13:10

Simon Baslé


here it is checked that onNext is not called

 StepVerifier.create(result).expectNextCount(0).verifyComplete()
like image 38
samibel Avatar answered Oct 02 '22 13:10

samibel