I am using test container for my Integration Test in a Spring Boot app and trying to connect database in the composed Docker container.
However, I cannot connect even using the given url and credentials in my properties file:
spring:
datasource:
driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver
url: jdbc:tc:postgresql:14.6-alpine:///employees_db
username: sa
password: 123456
On the other hand, when I debug the test, there is completely a different url on the console with a different database name.
Database: jdbc:postgresql://localhost:64880/test (PostgreSQL 14.6)
I tried to connect using both of them via Host and Url connection of DBeaver, but get error ("authentication failed or connection refused errors).
So, how can I connect this database during debugging a test method (pausing with breakpoint).
If it is still relevant for you, I faced the same issue.
My application-test.yml
:
spring:
jpa:
hibernate:
ddl-auto: validate
datasource:
driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver
url: jdbc:tc:postgresql:9.6.8://hostname/pc_db?TC_DAEMON=true
and my console output during the test run:
Database: jdbc:postgresql://localhost:52957/pc_db (PostgreSQL 9.6)
finally I was able to connect the test DB with these properties:
jdbc:postgresql://localhost:52957/pc_db
test
test
🙂
Note that the DB port is generated randomly on each test run! And each time you should take it from the test logs!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With