Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect database in Testcontainer during debugging?

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).

like image 848
Jack Avatar asked Oct 19 '25 00:10

Jack


1 Answers

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:

  • URL: jdbc:postgresql://localhost:52957/pc_db
  • User: test
  • Password: test

🙂

Note that the DB port is generated randomly on each test run! And each time you should take it from the test logs!

like image 67
provisota Avatar answered Oct 21 '25 14:10

provisota