{"id":18329621,"url":"https://github.com/jupiter-tools/spring-dynamic-property","last_synced_at":"2025-08-02T21:33:33.264Z","repository":{"id":57725721,"uuid":"196213552","full_name":"jupiter-tools/spring-dynamic-property","owner":"jupiter-tools","description":"Tools to define a dynamic property in Spring Boot integration tests.","archived":false,"fork":false,"pushed_at":"2019-07-18T07:48:04.000Z","size":34,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-06T21:41:51.819Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://jupiter-tools.com","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/jupiter-tools.png","metadata":{"files":{"readme":"README.adoc","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":"2019-07-10T13:47:31.000Z","updated_at":"2022-04-10T15:39:07.000Z","dependencies_parsed_at":"2022-09-02T03:41:12.702Z","dependency_job_id":null,"html_url":"https://github.com/jupiter-tools/spring-dynamic-property","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/jupiter-tools/spring-dynamic-property","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jupiter-tools%2Fspring-dynamic-property","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jupiter-tools%2Fspring-dynamic-property/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jupiter-tools%2Fspring-dynamic-property/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jupiter-tools%2Fspring-dynamic-property/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jupiter-tools","download_url":"https://codeload.github.com/jupiter-tools/spring-dynamic-property/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jupiter-tools%2Fspring-dynamic-property/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268456752,"owners_count":24253278,"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","status":"online","status_checked_at":"2025-08-02T02:00:12.353Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-11-05T19:18:05.013Z","updated_at":"2025-08-02T21:33:33.190Z","avatar_url":"https://github.com/jupiter-tools.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Spring Dynamic Property\n\nimage:https://travis-ci.com/jupiter-tools/spring-dynamic-property.svg?branch=master[\"Build Status\", link=\"https://travis-ci.com/jupiter-tools/spring-dynamic-property\"]\nimage:https://codecov.io/gh/jupiter-tools/spring-dynamic-property/branch/master/graph/badge.svg[link=\"https://codecov.io/gh/jupiter-tools/spring-dynamic-property\"]\n\n\nTools to define a dynamic property in Spring Boot integration tests.\n\n## How to set a property value in tests by the standard spring mechanics\n\nIn the Spring Framework we have an annotation `TestPropertySource` which allows\ndefine a final property value:\n\n[source, java]\n----\n@SpringBootTest\n@ExtendWith(SpringExtension.class)\n@TestPropertySource(properties = \"my.variable=12345\")\nclass FinalPropertyValueTest {\n\n\t@Value(\"${my.variable}\")\n\tprivate String variable;\n\n\t@Test\n\tvoid testVariableValue() {\n\t\tassertThat(variable).isEqualTo(\"12345\");\n\t}\n}\n----\n\nOn this way we have a small limitation -\nwe cannot define a dynamic value of properties in this annotation,\neven use of a static method not allowed here.\n\n## How to define a dynamic value of properties in tests\n\n`spring-dynamic-property` supports an ability to define a dynamic value of properties and\ndeclare static property provider methods in your test class.\n\nAll that you need to use the `spring-dynamic-property` is the next dependency:\n\n[source,xml]\n----\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.jupiter-tools\u003c/groupId\u003e\n    \u003cartifactId\u003espring-dynamic-property\u003c/artifactId\u003e\n    \u003cversion\u003e0.1\u003c/version\u003e\n\u003c/dependency\u003e\n----\n\nLet's look at the simple example of using a dynamic value in properties:\n\n### Simple example\n\n[source, java]\n----\n@SpringBootTest\n@ExtendWith(SpringExtension.class)\nclass SimpleTest {\n\n\t@DynamicTestProperty\n\tprivate static TestPropertyValues props() {\n\t\treturn TestPropertyValues\n\t\t\t\t.of(\"variable=\" + (int) Math.sqrt(64));\n\t}\n\n\t@Value(\"${variable}\")\n\tprivate int variable;\n\n\t@Test\n\tvoid testSqrt() {\n\t\tassertThat(variable).isEqualTo(8);\n\t}\n}\n----\n\n### Example with the using of TestContainers\n\nA most primary case to use dynamic properties is a TestContainers\ninitialization in your spring-boot applications.\n\nLet's consider an example of initialization standard spring-boot properties\nafter start Redis test container:\n\n[source, java]\n----\n@SpringBootTest\n@Testcontainers\n@ExtendWith(SpringExtension.class)\nclass RedisTestcontainersTest {\n\n    private static final Integer REDIS_PORT = 6379;\n\n    @Container\n    private static GenericContainer redis = new GenericContainer(\"redis:latest\")\n            .withExposedPorts(REDIS_PORT);\n\n    @DynamicTestProperty\n    private static TestPropertyValues props() {\n        return TestPropertyValues.of(\"spring.redis.host=\" + redis.getContainerIpAddress(),\n                                     \"spring.redis.port=\" + redis.getMappedPort(REDIS_PORT));\n    }\n\n    @Autowired\n    private RedisTemplate redisTemplate;\n\n    @Test\n    void readWriteValueByRedisTemplate() {\n        String key = \"test\";\n        String value = \"sabracadabra\";\n        // Act\n        redisTemplate.opsForValue().set(key, value);\n        // Assert\n        assertThat(redisTemplate.opsForValue().get(key)).isEqualTo(value);\n    }\n}\n----\n\n## Include dynamic property in meta-annotations\n\nIf you need to make you own annotation with dynamic property declaration\nthen you can specify a file with `@DynamicProperty` methods\nby the using of `@IncludeDynamicProperty` annotation\nas you can see in the example below:\n\n[source, java]\n----\n@Target(ElementType.TYPE)\n@Retention(RetentionPolicy.RUNTIME)\n@IncludeDynamicProperty(DynamicPropertyHolder.class)\npublic @interface AnnotationWithDynamicProperty {\n\n}\n----\n\nthe file with dynamic properties:\n\n[source, java]\n----\npublic class DynamicPropertyHolder {\n\n    @DynamicTestProperty\n    private static TestPropertyValues property(){\n        return TestPropertyValues.of(\"key=12345\");\n    }\n}\n----\n\nand now you can use this annotation in your tests to set this property:\n\n[source, java]\n----\n@SpringBootTest\n@AnnotationWithDynamicProperty\nclass MetaAnnotationTest {\n\n    @Value(\"${key}\")\n    private String key;\n\n    @Test\n    void checkPropertyValueInjectInAnnotation() {\n        assertThat(key).isEqualTo(\"12345\");\n    }\n}\n----\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjupiter-tools%2Fspring-dynamic-property","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjupiter-tools%2Fspring-dynamic-property","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjupiter-tools%2Fspring-dynamic-property/lists"}