{"id":22152134,"url":"https://github.com/findinpath/postgres-spring-boot-dynamicpropertysource","last_synced_at":"2025-06-23T18:34:32.271Z","repository":{"id":43851450,"uuid":"250959472","full_name":"findinpath/postgres-spring-boot-dynamicpropertysource","owner":"findinpath","description":"Proof of concept for using the `DynamicPropertySource` spring annotation in tests requiring PostgreSQL","archived":false,"fork":false,"pushed_at":"2022-09-08T01:06:57.000Z","size":10,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-24T13:27:22.089Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.findinpath.com/spring-boot-dynamicpropertysource-postgres/","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/findinpath.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-03-29T05:18:21.000Z","updated_at":"2023-05-17T13:42:09.000Z","dependencies_parsed_at":"2023-01-18T00:00:51.515Z","dependency_job_id":null,"html_url":"https://github.com/findinpath/postgres-spring-boot-dynamicpropertysource","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/findinpath/postgres-spring-boot-dynamicpropertysource","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/findinpath%2Fpostgres-spring-boot-dynamicpropertysource","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/findinpath%2Fpostgres-spring-boot-dynamicpropertysource/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/findinpath%2Fpostgres-spring-boot-dynamicpropertysource/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/findinpath%2Fpostgres-spring-boot-dynamicpropertysource/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/findinpath","download_url":"https://codeload.github.com/findinpath/postgres-spring-boot-dynamicpropertysource/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/findinpath%2Fpostgres-spring-boot-dynamicpropertysource/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261534346,"owners_count":23173385,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-02T00:47:20.878Z","updated_at":"2025-06-23T18:34:32.228Z","avatar_url":"https://github.com/findinpath.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"Proof of concept for using the `DynamicPropertySource` spring annotation in tests requiring PostgreSQL\n======================================================================================================\n\nThis simple project shows how to work with the newly introduced\n`org.springframework.test.context.DynamicPropertySource` annotation which\ncan be used in spring tests that make use of [testcontainers](https://www.testcontainers.org/).\n\n\nDetails about the process that led to the introduction of the `DynamicPropertySource` annotation can\nbe found in the Github issue:\n\nhttps://github.com/spring-projects/spring-framework/issues/24540\n\nBefore introducing this  annotation, in order to interact via spring boot with a data source \n(backed by a test container) that was mapped via the `spring.datasource` in the `application.yml` file (for triggering via \n`org.springframework.boot.autoconfigure.jdbc.DataSourceInitializationConfiguration` the creation of the much needed\nspring bean instance of type `javax.sql.DataSource` for JDBC/JPA tests), there was needed an\nimplementation of the `ApplicationContextInitializer` to introduce the required properties in\nthe configurable application context:\n\n```java\n@SpringJUnitConfig(classes = {Application.class}, initializers = {PostgresIntegrationTest.Initializer.class})\n@Testcontainers\npublic class PostgresIntegrationTest {\n\n  @Container\n  public static PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer(\"postgres:12\")\n      .withDatabaseName(\"integration-tests-db\")\n      .withUsername(\"sa\")\n      .withPassword(\"sa\");\n\n\n  @Autowired\n  private JdbcTemplate jdbcTemplate;\n\n  static class Initializer implements ApplicationContextInitializer\u003cConfigurableApplicationContext\u003e {\n    public void initialize(ConfigurableApplicationContext configurableApplicationContext) {\n      TestPropertyValues.of(\n          \"spring.datasource.url=\" + postgreSQLContainer.getJdbcUrl(),\n          \"spring.datasource.username=\" + postgreSQLContainer.getUsername(),\n          \"spring.datasource.password=\" + postgreSQLContainer.getPassword()\n      ).applyTo(configurableApplicationContext.getEnvironment());\n    }\n  }\n \n  @Test\n  public void demo(){\n     jdbcTemplate.execute(\"SELECT 1\");\n  }\n}\n```\n\nWith the introduction of the `@DynamicPropertySource` there is no need for an extra `ApplicationContextInitializer`:\n\n```java\n@SpringJUnitConfig(classes = {Application.class})\n@Testcontainers\npublic class PostgresIntegrationTest {\n\n  @Container\n  public static PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer(\"postgres:12\")\n      .withDatabaseName(\"integration-tests-db\")\n      .withUsername(\"sa\")\n      .withPassword(\"sa\");\n\n\n  @Autowired\n  private JdbcTemplate jdbcTemplate;\n\n  @DynamicPropertySource\n  static void postgresProperties(DynamicPropertyRegistry registry) {\n    registry.add(\"spring.datasource.url\", postgreSQLContainer::getJdbcUrl);\n    registry.add(\"spring.datasource.username\", postgreSQLContainer::getUsername);\n    registry.add(\"spring.datasource.password\", postgreSQLContainer::getPassword);\n  }\n\n  @Test\n  public void demo() {\n    jdbcTemplate.execute(\"SELECT 1\");\n  }\n}\n```\n\nAs can be seen from above, the newly introduced `@DynamicPropertySource` is somehow similar to the \ncommonly used `@TestPropertySource` annotation with the mention that it allows the usage of dynamic resources\nsuch as the IP and port assigned to the container (needed in the `jdbcUrl` in the example above).\n\nCheck out the full source code (and corresponding documentation) of \nthe [PostgresIntegrationTest.java](src/test/java/com/findinpath/springboot/testcontainers/PostgresIntegrationTest.java) class. \n\n\nSee details about the usage of the `@DynamicPropertyResource` in the Spring framework \n[documentation](https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/testing.html#testcontext-ctx-management-dynamic-property-sources).\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffindinpath%2Fpostgres-spring-boot-dynamicpropertysource","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffindinpath%2Fpostgres-spring-boot-dynamicpropertysource","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffindinpath%2Fpostgres-spring-boot-dynamicpropertysource/lists"}