Search code examples
javaunit-testingmockitoreactive-programmingspring-webflux

How to mock a method that returns `Mono<Void>`


How to mock a method that returns Mono<Void>?

I have this method that returns Mono<Void>

public Mono<Void> deleteMethod(Post post) {

        return statusRepository.delete(post);
    }

In my test class I want to do something like this

given(statusRepository.delete(any(Post.class))).willReturn(Mono.empty());

Is there any better way to do this?

Can someone help me?

Thanks.


Solution

  • This is possible using Mockito.when:

    Mockito.when(statusRepository.delete(any(Post.class)).thenReturn(Mono.empty());
    

    ...call the method and verify...

    Mockito.verify(statusRepository).delete(any(Post.class));