繁体   English   中英

无法使用 spring 引导运行 r2dbc 测试

[英]Unable to run test for r2dbc with spring boot

我正在关注链接以使用 spring 数据实现 R2DBC。 该项目具有巨大的 POM,因为它还演示了其他功能。 因此,我尝试仅使用 spring 引导和带有 H2 的 R2DBC 所需的依赖项。

我的pom是:

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

然后我将存储库定义为:

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

特征是实体 class。

我在 src/test/resources 的 application.properties 中配置了 h2:

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

在测试中:

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

    @Autowired
    ReactiveFeatureRepository rfr;

    @Autowired
    DatabaseClient client;

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

但是当我尝试运行测试时,我会得到巨大的日志,其中列出了“正匹配”和“负匹配”,最后:

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 

与示例项目相比,我缺少什么?

对于 Spring 数据 R2dbc,数据库连接配置前缀为spring.r2dbc

在这里查看我的示例

如果类路径中有 H2 R2dbc 驱动程序,您可以忽略 H2/R2dbc 配置。

一个用于测试 H2 存储库的示例

对于其他数据库支持和使用 TestContainers 编写测试代码,请查看此存储库中的其他 r2dbc 示例。

最新的 R2dbc 有一个新的存储库,它演示了即将推出的 Spring Boot 2.4/Spring Data R2dbc/Spring 5.3 R2dbc 的功能。

暂无
暂无

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

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