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 ?
If you are using Spring Boot, combining
@DataR2dbcTestand testcontainers is easy to write db related tests against a real database running in Docker container.The example test using
@DataR2dbcTestand 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