🤗 ApiHug × {Postman|Swagger|Api...} = 快↑ 准√ 省↓
- GitHub - apihug/apihug.com: All abou the Apihug
- apihug.com: 有爱,有温度,有质量,有信任
- ApiHug - API design Copilot - IntelliJ IDEs Plugin | Marketplace
前面的例子中都使用了 System.out.println
或者 log.debug
, 或者打开 Flux.log()
方法,输出数据的用 来验证程序。
这种方式需要人工去验证结果是否正确并且无法在单元测试中使用, Reactor为测试准备了相关的工具库reactor-test。
引入reactor-test依赖,我们这里在main中使用reactor-test中工具类:
testImplementation 'io.projectreactor:reactor-test'
reactor-test
三个主要:
- StepVerifier 用来测试序列步骤。
- TestPublisher 生成测试数据。
- In sequences that can go through several alternative Publisher (for example, a chain that uses switchIfEmpty, probing such a Publisher to ensure it was used (that is, subscribed to).
#StepVerifier
reactor-test核心接口为 StepVerifier
,该接口提供了若干的静态工厂方法,从待测试的Publisher创建测试步骤。测试步骤被抽象为状态机接口FirstStep、Step和LastStep,分别代表了测试的初始阶段、中间阶段和最终阶段。这些Step上都具有一系列的expect和assert方法,用于测试当前状态是否符合预期。
使用StepVerifier测试Publisher的套路如下:
- 首先将已有的Publisher传入StepVerifier的create方法。
- 多次调用expectNext、expectNextMatches方法设置断言,验证Pu