2

I am following this link to implement R2DBC with spring data. That project has huge POM as it demos other features too. So I try to have only dependencies I need for spring boot and R2DBC with H2.

My pom is as:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!--For testing-->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.199</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- junit 5 -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <scope>test</scope>
        </dependency>

        <!--For reactive-->
        <dependency>
            <groupId>io.r2dbc</groupId>
            <artifactId>r2dbc-h2</artifactId>
            <version>0.8.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-r2dbc</artifactId>
            <version>1.0.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-core</artifactId>
        </dependency>
        <!---->
<dependencies>      

I have then defined repository as:

public interface ReactiveFeatureRepository extends ReactiveCrudRepository<Feature, UUID> {
}

Feature is the entity class.

I have configured h2 in application.properties in src/test/resources:

spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa

In test:

@ExtendWith(SpringExtension.class)
@SpringBootTest
public class ReactiveFeatureRepositoryTest {

    @Autowired
    ReactiveFeatureRepository rfr;

    @Autowired
    DatabaseClient client;

    @Autowired
    H2ConnectionFactory factory;
...
...
}

But when I try to run test, I get huge logs which list 'Poitive matches' and 'Negative matches', and finally:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 
'x.y.z.ReactiveFeatureRepositoryTest': Unsatisfied dependency expressed 
through field 'rfr'; nested exception is 
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 
'x.y.z.ReactiveFeatureRepository' available: expected at least 1 bean which 
qualifies as autowire candidate. Dependency annotations: 
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at 

As compared to sample project, what am I missing?

Mandroid
  • 6,200
  • 12
  • 64
  • 134

1 Answers1

3

For Spring Data R2dbc, the db connection config prefix is spring.r2dbc.

Check my example here

You can ignore the H2/R2dbc config if there is H2 R2dbc driver in the classpath.

There is a example for testing Repository for H2.

For other database supports and writing testing codes with TestContainers, check other r2dbc examples in this repository.

Update: There is a new R2dbc Examples Project, which demos R2dbc feature in Spring Boot 2.4+/Spring Data R2dbc/Spring 5.3 R2dbc module.

Hantsy
  • 8,006
  • 7
  • 64
  • 109
  • Half of your links are now dead. – desertnaut Oct 31 '22 at 00:23
  • @desertnaut R2dbc itself is updated frequently, the old examples were moved to `legacy` folder(all links are updated now). To use the latest R2dbc, check the [R2dbc Examples](https://github.com/hantsy/spring-r2dbc-sample). – Hantsy Oct 31 '22 at 03:41