java - Spring WebFlux,单元测试Mono和Flux

标签 java unit-testing spring-boot reactive-programming spring-webflux

对响应式编程感兴趣,我玩了一下 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 a WebTestClient that can be used to test WebFlux server endpoints with or without a running server.

-- https://docs.spring.io/spring/docs/5.0.0.BUILD-SNAPSHOT/spring-framework-reference/html/web-reactive.html#web-reactive-tests

关于java - Spring WebFlux,单元测试Mono和Flux,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51830411/

相关文章:

java - Spring Boot 应用程序上的 Maven 构建失败

c# - 在 NUnit 2.5 中使用 Lambda 作为约束?

java - 错误 : Weblogic Maven Plugin deployment

java - Axis2 不返回自己的对象

java - SnakeYAML 保存不正确

c# - 集成测试策略资源

vb.net - 使用 Microsoft Fakes 模拟数据库

java - Spring Boot和Spring Cloud配置客户端错误

java - 如何通过从 java 类传递参数来调用 jar

java - 自动调用父类(super class)方法