简体   繁体   English

如何模拟返回`Mono的方法<Void> `

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

How to mock a method that returns Mono<Void> ?如何模拟返回Mono<Void>的方法?

I have this method that returns Mono<Void>我有这个返回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.谢谢。

This is possible using Mockito.when :这可以使用Mockito.when

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

...call the method and verify... ...调用方法并验证...

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

I was also able to do this without using Mono.empty so the reactive chain would complete by creating a mock object of type void.我也可以在不使用Mono.empty的情况下做到这一点,因此反应链将通过创建一个 void 类型的模拟对象来完成。 Below is a code example (written in Kotlin and using mockito-kotlin, but should be applicable to mockito as well):下面是一个代码示例(用 Kotlin 编写并使用 mockito-kotlin,但也应该适用于 mockito):

val mockVoid: Void = mock()

whenever(statusRepository.delete(any(Post::class.java)).thenReturn(mockVoid)

当我不想得到一个空的单声道时,我会这样做。

when(statusRepository.delete(any(Post.class))).thenReturn(Mono.just("").then());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM