Spring boot 反应堆switchifempty的行为与junit测试中的预期不符

Spring boot 反应堆switchifempty的行为与junit测试中的预期不符,spring-boot,mockito,kotlin,project-reactor,Spring Boot,Mockito,Kotlin,Project Reactor,我正在为下面提供的方法编写测试。 ` 测试失败,因为调用了cache.remove(request.link)。据我所知(或根据我从文档中收集到的信息),只有当recipeService.findByHash返回Mono.empty()时,才应该触发switchIfEmpty。然而,调试器显示它返回Mono.just(fetchedRecipe)的模拟值 有趣的是当我替换 .switchIfEmpty(cache.remove(request.link).then(Mono.empty()))

我正在为下面提供的方法编写测试。 `

测试失败,因为调用了cache.remove(request.link)。据我所知(或根据我从文档中收集到的信息),只有当recipeService.findByHash返回Mono.empty()时,才应该触发switchIfEmpty。然而,调试器显示它返回Mono.just(fetchedRecipe)的模拟值

有趣的是当我替换

.switchIfEmpty(cache.remove(request.link).then(Mono.empty()))

然后不打印weee,因此其行为与预期一致,即switchIfEmpty未激发

此外,测试问题在集成测试中正常运行,并且不会清除缓存

反应堆版本:3.1.0-RC1 其他值得注意的细节:SpringBoot2.0.0-M4,Mockito内核:2.10,JUnit5,项目是用kotlin编写的


问题是,有人认为这有什么问题吗?因为我已经花了两天的时间,仍然不知道为什么这样做会如此奇怪。

最后我发现了如何让它工作

为了补救这种情况:

 override fun provide(request: ScrapingRequest): Flux<ScrapedRecipe> =
        cache.retrieve(request.link)
                .flatMap { (link, _, recipeHash, error) ->
                    recipeService.findByHash(recipeHash)
                            .map { ScrapedRecipe(it, link, error) }
                            .switchIfEmpty(Mono.just(1)
                                    .flatMap { cache.remove(request.link) }
                                    .then(Mono.empty()))
                }
                .flux()
override-fun-provide(请求:ScrapingRequest):流量=
cache.retrieve(request.link)
.flatMap{(链接、错误、recipeHash)->
recipeService.findByHash(recipeHash)
.map{ScrapedRecipe(it、链接、错误)}
.switchIfEmpty(单声道)just(1)
.flatMap{cache.remove(request.link)}
.然后(Mono.empty())
}
.flux()
您可以看到使用flatMap执行异步工作是如何完成这项工作的,即使这不是最整洁的实现,它也向我揭示了隐藏在这里的一个非常有趣的机制

.switchIfEmpty(cache.remove(request.link).then(Mono.empty()))
.switchIfEmpty(Mono.just(1).doOnNext{println("weeee")}.then(Mono.empty()))
 override fun provide(request: ScrapingRequest): Flux<ScrapedRecipe> =
        cache.retrieve(request.link)
                .flatMap { (link, _, recipeHash, error) ->
                    recipeService.findByHash(recipeHash)
                            .map { ScrapedRecipe(it, link, error) }
                            .switchIfEmpty(Mono.just(1)
                                    .flatMap { cache.remove(request.link) }
                                    .then(Mono.empty()))
                }
                .flux()