R2dbc integration test persisted data not available via API

179 Views Asked by At

I am trying to learn Spring web flux with r2dbc. I am trying to execute a test case as follows:

  1. Save the data by calling the save method directly via a repository bean.
  2. Fetch the saved data by invoking the API using the webflux test client.

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

1

There are 1 best solutions below

0
On

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. I blocked it with 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();
    }