") .fetch() .all(); I am conn" /> ") .fetch() .all(); I am conn" /> ") .fetch() .all(); I am conn"/>

How to write junit for r2dbc for postgres database?

128 Views Asked by At

I have a class which uses used R2dbcEntityTemplate as shown below

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

I am connecting to a postgres database, properties of which i am supplying through application.properties.

How do i write JUNIT for this class ?

1

There are 1 best solutions below

0
Hantsy On

If you are using Spring Boot, combining @DataR2dbcTest and testcontainers is easy to write db related tests against a real database running in Docker container.

The example test using @DataR2dbcTest and testcontainers from my github:

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

For none Spring Boot, it is also easy to prepare a test purposed config, and run on testcontainers via ApplicationContextInitializer.

Check my example: https://github.com/hantsy/spring6-sandbox/blob/master/r2dbc/src/test/java/com/example/demo/domain/PostRepositoryTest.java