对响应式编程感兴趣,我玩了一下 Building a Reactive RESTful Web Service指导。并想继续前进并添加一些单元测试。
我尝试使用普通 Junit/Mockito 测试来测试我的处理程序 ( RouterFunction
)。但是,因为它是响应式(Reactive)的,处理程序返回一个 Mono<ServerResponse>
。 .所以我不得不 block()
它来测试 ServerResponse
状态,但无法提取他的 body 进行测试。在网上搜索解决方案时,似乎所有示例都使用了 WebTestClient
.
我的问题是:
假设所有样本都使用 WebTestClient
测试 react 性 REST 服务并且(单元)测试 ServerResponse
的主体并不容易.对 RouterFunction
进行单元测试是一个好习惯吗?还是始终使用 WebTestClient
进行更广泛的测试更好? ?
非常感谢。
最佳答案
似乎最好的做法是使用WebTestClient
。然而,这个可以在没有运行服务器的情况下使用;
The
spring-test
module includes aWebTestClient
that can be used to test WebFlux server endpoints with or without a running server.
关于java - Spring WebFlux,单元测试Mono和Flux,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51830411/