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

gorkyyrv  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(61)

我有一个类,它使用了R2dbcEntityTemplate,如下所示

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

字符串
我正在连接到一个postgres数据库,通过application.properties提供其属性。
如何为这个类编写JUNIT?

lymgl2op

lymgl2op1#

如果你使用的是Sping Boot ,结合@DataR2dbcTest和testcontainers可以很容易地针对Docker容器中运行的真实的数据库编写db相关的测试。
使用github上的@DataR2dbcTest和testcontainers进行的示例测试:
https://github.com/hantsy/spring-r2dbc-sample/blob/master/boot/src/test/java/com/example/demo/PostRepositoryTest.java
对于非Sping Boot ,也可以轻松准备测试配置,并通过ApplicationContextInitializer在测试容器上运行。
查看我的示例:https://github.com/hantsy/spring6-sandbox/blob/master/r2dbc/src/test/java/com/example/demo/domain/PostRepositoryTest.java

相关问题