-1

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

Munish Dhiman
  • 511
  • 1
  • 7
  • 22
  • This is way too little code for anyone to answer. We have no idea what your service looks like and this is impossible to reproduce. Please read the ”how to ask a good question” section of stack overflow and then put som effort into your question. – Toerktumlare Apr 30 '22 at 18:49
  • 1
    If itemRepository is a reactive repository, then you should make sure to call a `block()` or `blockLast()` after saveAll to trigger the execution. – Martin Tarjányi May 01 '22 at 04:41
  • @MartinTarjányi, Thank you for this hint. This is what I was missing. – Munish Dhiman May 01 '22 at 05:21
  • 1
    @Toerktumlare, I thought Spring web flux and r2dbc could be enough to understand that it is a reactive repository method that is saving the data to DB and found it unnecessarily bloating of the question to put that code over here. Thank you too for your suggestion. I will read that section again :) – Munish Dhiman May 01 '22 at 05:29
  • 1
    @MunishDhiman read martins first comment. `if this is` That was not clear from the code provided, you wrote what the application was. Not what the unit teasts was using. Martin did an assumption, which should have not been needed if you had supplied more code, and actually supplying the code used. Please think about that to next time. – Toerktumlare May 01 '22 at 09:58

1 Answers1

0

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();
    }
Munish Dhiman
  • 511
  • 1
  • 7
  • 22