简体   繁体   English

R2dbc 集成测试持久数据无法通过 API 获得

[英]R2dbc integration test persisted data not available via API

I am trying to learn Spring web flux with r2dbc.我正在尝试使用 r2dbc 学习 Spring web 通量。 I am trying to execute a test case as follows:我正在尝试按如下方式执行测试用例:

  1. Save the data by calling the save method directly via a repository bean.通过存储库 bean 直接调用 save 方法来保存数据。
  2. Fetch the saved data by invoking the API using the webflux test client.通过使用 webflux 测试客户端调用 API 来获取保存的数据。

Step two does not return any data while the two records were getting inserted successfully.成功插入两条记录时,第二步不返回任何数据。

    @Test
    void searchItem() {
        // Arrange
        itemRepository.saveAll(createItemEntitiesToInsert());
        // Act
        final var responseBody = get("/api/v1/items/search?name=ite")
            .returnResult(ItemResource.class)
            .getResponseBody();
        // Assert
        StepVerifier.create(responseBody)
            .expectNextCount(2)
            .expectComplete()
            .verify();
    }

Looks like some context-related issue but not sure what is happening:(看起来像一些与上下文相关的问题,但不确定发生了什么:(

Thanks to Martin ,感谢马丁

After calling the saveAll method of the repository, I was not blocking the saveAll method and was immediately going for fetching the data with APIs.调用存储库的saveAll方法后,我没有阻止saveAll方法,而是立即使用 API 获取数据。 I blocked it with saveAll(items).blockLast() .我用saveAll(items).blockLast()阻止了它。 So the correct code should be:所以正确的代码应该是:

    @Test
    void searchItem() {
        // Arrange
        itemRepository.saveAll(TestHelper.createItemEntitiesToInsert()).blockLast();
        // Act
        final var responseBody = get("/api/v1/items/search?name=ite")
            .returnResult(ItemResource.class)
            .getResponseBody();
        // Assert
        StepVerifier.create(responseBody)
            .expectNextCount(2)
            .expectComplete()
            .verify();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM