【发布时间】:2021-02-24 15:48:11
【问题描述】:
我有一个 Spring Boot 测试,其代码创建了一个简单的 Flux<String 异步结果。我想测试是否有一个包含字符串price 的结果。代码是这样的:
@SpringBootTest
@ContextConfiguration(classes = PriceTests.class)
public class PriceTests {
@Autowired
RestController controller;
@Test
public void DoPriceTests() throws InterruptedException
{
Flux<String> prices = controller.getPrices();
Logs.Info("Test prices");
StepVerifier.create(prices)
.expectNextMatches(s -> s.contains("price"))
.expectComplete()
.verify(Duration.ofSeconds(3));
}
}
问题是我注意到当没有价格时,测试仍然通过!那不应该发生。我怎样才能log expectNextMatches 字符串?例如,类似这样的内容将显示每个价格的Flux<String> 的实际值是多少:
StepVerifier.create(prices)
.expectNextMatches(s -> {
Logs.Info(s);
s.contains("price");
})
.expectComplete()
.verify(Duration.ofSeconds(3));
【问题讨论】:
-
空通量不是错误,不应被视为错误。如果你想让它失败,你需要 controller.getPrices() 在空时返回一个错误。
-
空通量不包含字符串“price”,正如 expectNextMatches 测试谓词所期望的那样?
-
StepVerifier.create(Flux.empty()).expectNextMatches(s -> s.equals("hello")).expectComplete().verify(Duration.ofSeconds(3));此代码按预期失败。是不是你的生产代码有问题?
标签: java spring-webflux project-reactor reactor