在junit测试中,Reactor switchifempty的行为与预期不符

时间:2017-10-08 12:00:06

标签: spring-boot mockito kotlin project-reactor

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

class ScrapedRecipeCache @Autowired constructor(private val cache: RecipeScrapingCacheService,
                                                private val recipeService: RecipeService) : ScrapedRecipeProvider {
    override fun provide(request: ScrapingRequest): Flux<ScrapedRecipe> =
            cache.retrieve(request.link)
                    .doOnNext { println(it) }
                    .flatMap { (link, _, recipeHash, error) ->
                        recipeService.findByHash(recipeHash)
                                .map { ScrapedRecipe(it, link, error)}
                                .switchIfEmpty(cache.remove(request.link).then(Mono.empty()))
                    }
                    .flux()


}

`     测试如下:

private val recipeFetched = Recipe("Tortellini", RecipeDifficulty.EASY, 15.0)

val cacheContents = RecipeScrapingResource("www.google.com", ScrapingOrigin.JAMIE_OLIVER, recipeFetched.hash,
                    mutableListOf(
                            pl.goolash.core.Exception("aa", ErrorType.WARNING, LocalDateTime.MIN)
                    ))
val request = ScrapingRequest("www.google.com", ScrapingOrigin.JAMIE_OLIVER, 4)

@BeforeEach
fun setUp() {
given(cache.retrieve("www.google.com")).willReturn(Mono.just(cacheContents))
given(recipeService.findByHash(recipeFetched.hash)).willReturn(Mono.just(recipeFetched))
}

@Test
@DisplayName("Then return data fetched from service and don't delete cache")
 fun test() {
      cacheFacade.provide(request)
                            .test()
                            .expectNext(ScrapedRecipe(recipeFetched, "www.google.com", cacheContents.error!!))
                            .expectComplete()
                            .verify()
      BDDMockito.verify(cache, BDDMockito.never()).remove(request.link)
                }

测试失败,因为调用了cache.remove(request.link)。根据我的理解(或者我从文档中收集的内容)switchIfEmpty,只应在recipeService.findByHash返回Mono.empty()时触发。但是调试器显示它返回Mono.just(fetchedRecipe)的模拟值。

有趣的是,当我替换

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

.switchIfEmpty(Mono.just(1).doOnNext{println("weeee")}.then(Mono.empty()))

然后weee没有被打印,因此它的行为符合预期,即switchIfEmpty没有被触发。

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

反应堆版本:3.1.0-RC1 其他值得注意的细节:Spring Boot 2.0.0-M4,Mockito-core:2.10,junit 5,项目是用kotlin编写的

问题是,有人看到这个有什么问题吗?因为我已经花了两天时间而仍然不知道为什么这样做会如此眩目。

1 个答案:

答案 0 :(得分:1)

最后,我发现了如何使这项工作。

为了解决这个问题:

 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()

你可以看到使用flatMap执行asynch工作是如何做到的,即使这不是最好的实现,它向我揭示了隐藏在这里的一个有趣的机制。