Search code examples
javaspring-bootintegration-testingtestcontainers

How do I load properties from application file for Testcontainer?


Currently, I am using Testcontainer to implement integration testing for database in spring boot. How do I init the Testcontainer with the application.yml. I don't want to use @DynamicPropertySource.

public static PostgreSQLContainer container = new PostgreSQLContainer("postgres:12")
spring:
  datasource:
    driverClassName: org.postgresql.Driver
    url: jdbc:postgresql://localhost:5432/db
    username: user
    password: password

Solution

  • You can simply add tc to you spring.datasource.url property.

    spring:
      datasource:
        driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver
        url: jdbc:tc:postgresql:12:///db?TC_TMPFS=/testtmpfs:rw
        username: user
        password: password
    

    I prefer not to use application.yaml just create application-test.yaml and override default one. You can check https://www.testcontainers.org/modules/databases/jdbc/.

    Also you can override with @testpropertysource annotation.