如何为postgres数据库的r2dbc编写junit?

问题描述 投票:0回答:1

我有一门使用二手

R2dbcEntityTemplate
的课程,如下所示

r2dbcTemplate
        .getDatabaseClient()
        .sql("<Custom-Query>")
        .fetch()
        .all();

我正在连接到一个 postgres 数据库,我通过 application.properties 提供该数据库的属性。

我如何为这门课编写JUNIT?

junit spring-webflux junit5 r2dbc r2dbc-postgresql
1个回答
0
投票

如果您使用 Spring Boot,结合

@DataR2dbcTest
和 testcontainers 可以轻松针对 Docker 容器中运行的真实数据库编写与数据库相关的测试。

使用

@DataR2dbcTest
和我的 github 中的 testcontainers 进行示例测试:

https://github.com/hantsy/spring-r2dbc-sample/blob/master/boot/src/test/java/com/example/demo/PostRepositoryTest.java

对于没有 Spring Boot 的人来说,准备一个用于测试的配置也很容易,并通过

ApplicationContextInitializer
在测试容器上运行。

检查我的示例:https://github.com/hantsy/spring6-sandbox/blob/master/r2dbc/src/test/java/com/example/demo/domain/PostRepositoryTest.java

© www.soinside.com 2019 - 2024. All rights reserved.