javaspringspring-dataspring-data-r2dbcr2dbc

Equivalent to DataJpaTest for Spring Data's ReactiveCrudRepository and R2DBC


For JpaRepository there is @DataJpaTest. @DataJpaTest allows for simple and isolated testing of JPA repositories in Spring.

We are using spring-data-r2dbc. Is there an equivalent of @DataJpaTest for ReactiveCrudRepository to test it in isolation?


Solution

  • Yes, as of Spring Boot 2.3 you can use @DataR2dbcTest to spin up a context for your R2DBC repositories (including the DatabaseClient):

    @DataR2dbcTest
    class DataR2dbcTestIntegrationTests {
    
        @Autowired
        private DatabaseClient databaseClient;
    
        @Autowired
        private ConnectionFactory connectionFactory;
    
        @Autowired
        private MyRepository myRepository;
    
        @Test
        void testDatabaseClient() {
            this.databaseClient.execute("SELECT * FROM example").fetch().all()
                 .as(StepVerifier::create).verifyComplete();
        }
    
        // …
    }