{"id":16541250,"url":"https://github.com/itzg/try-sb-test-properties-loading","last_synced_at":"2026-05-11T17:35:06.726Z","repository":{"id":145318076,"uuid":"168608189","full_name":"itzg/try-sb-test-properties-loading","owner":"itzg","description":"This Spring Boot code demonstrates several ways of using a type-safe configuration class during unit testing","archived":false,"fork":false,"pushed_at":"2019-02-01T16:35:50.000Z","size":82,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-12-19T13:16:30.227Z","etag":null,"topics":["spring-boot","unit-testing"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/itzg.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-01-31T22:42:26.000Z","updated_at":"2024-12-30T15:46:03.000Z","dependencies_parsed_at":null,"dependency_job_id":"ad8d2f24-f6a0-46f1-9d5e-5ceca0d0e5f3","html_url":"https://github.com/itzg/try-sb-test-properties-loading","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/itzg/try-sb-test-properties-loading","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/itzg%2Ftry-sb-test-properties-loading","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/itzg%2Ftry-sb-test-properties-loading/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/itzg%2Ftry-sb-test-properties-loading/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/itzg%2Ftry-sb-test-properties-loading/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/itzg","download_url":"https://codeload.github.com/itzg/try-sb-test-properties-loading/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/itzg%2Ftry-sb-test-properties-loading/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32906267,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-11T17:09:15.040Z","status":"ssl_error","status_checked_at":"2026-05-11T17:08:45.420Z","response_time":120,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["spring-boot","unit-testing"],"created_at":"2024-10-11T18:54:30.912Z","updated_at":"2026-05-11T17:35:06.707Z","avatar_url":"https://github.com/itzg.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"This Spring Boot code demonstrates several ways of using a [type-safe configuration class](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-typesafe-configuration-properties)\nduring unit testing with properties set by various means.\n\nThe application itself does nothing useful. If you run it, the `LogIdentifierRunner` will simply\nlog the value of `app.identifier` that was loaded into the properties bean `AppProperties` that\nwas autowired into the service bean `UsesAppPropertiesService`. \n\nThis in itself demonstrates a feature of Spring Boot where it can [externalize configuration](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html)\nfrom several places including an `application.yml` (or `application.properties`) located in the \napplication's class path. In Maven/Gradle terms, that's simply the file \n`src/main/resources/application.yml` since the build will carry that over to the application's\nbuild artifact.\n\nTo give some background, the focal point of this repository is a type-safe properties class,\n`AppProperties`, which contains:\n\n```java\n@ConfigurationProperties(\"app\")\n@Component\n@Data\npublic class AppProperties {\n\n  /**\n   * The identifier our application wants.\n   */\n  String identifier = \"default\";\n}\n```\n\nThere's a lot going on in this one little, single-field class:\n- `@ConfigurationProperties(\"app\")` marks this as a type-safe configuration properties class that\n  will be bound to properties with the `app` prefix\n- `@Component` marks this class to be picked up by [component scanning](https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-classpath-scanning)\n  and registered into the Spring application context\n- `@Data` is a [Lombok](https://projectlombok.org/) annotation that gets processed during compilation\n  to make the data fields private and inject getter/setters. It also injects [a few other things](https://projectlombok.org/features/Data)\n- The field `identifier` is what our application wants to access. It will become bound to a \n  sub-property named for the field, `identifier`, and that binding will ensure it is a valid `String`.\n  It is declared with a default literal value, `\"default\"`. That default value, along with the\n  field's type and name, get picked up by [configuration processing](https://docs.spring.io/spring-boot/docs/current/reference/html/configuration-metadata.html)\n  and displayed by your IDE, etc.\n- The field description in the javadoc `/** ... */` also gets picked up by configuration processing \n  and is displayed in this IntelliJ quick docs tooltip:\n  \n  ![](docs/property-tooltip.png)\n\nThe bulk of this repository is actually the unit tests located in `src/main/test`. The following\nsections describe each strategy used to populate `AppProperties` during a unit test.\n\n## Full context\n\n```java\n@RunWith(SpringRunner.class)\n@SpringBootTest\n```\n\nThis is the easiest since a `@SpringBootTest` effectively initiates a full application context\nload along with all the externalized configuration loading and property binding.\n\nThe property value gets picked up from the `application.yml` in the class path, which originates\nfrom `src/main/resources`.\n\n## Full context using inline property values\n\n```java\n@RunWith(SpringRunner.class)\n@SpringBootTest(\n    properties = \"app.identifier=inline\"\n)\n```\n\nThis one overrides the value loaded from the classpath's `application.yml` with an inline property\nvalue.\n\n## Minimal context using defaults from property class\n\n```java\n@RunWith(SpringRunner.class)\n@ContextConfiguration(classes = {\n    UsesAppPropertiesService.class\n})\n@EnableConfigurationProperties(AppProperties.class)\n```\n\nAs discused [in the Spring Boot docs](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications-testing-autoconfigured-tests),\nsometimes you want and sometimes should test with a purposeful slice of your application's context.\nUnfortunately it takes a little more work to fine tune our test's context:\n\n- The `@ContextConfiguration` brings in our service to be \"tested\"\n- The `@EnableConfigurationProperties` activates the configuration binding of our specific class\n  and also registers it into the application context.\n  In this case, I could have just added `AppProperties` to the context configuration's `classes`;\n  however, I find `@EnableConfigurationProperties` more explicit and translates better to the other\n  options, below.\n\n## Minimal context using main `application.yml`\n\n```java\n@RunWith(SpringRunner.class)\n@ContextConfiguration(classes = {\n    UsesAppPropertiesService.class\n}, initializers = {\n    ConfigFileApplicationContextInitializer.class\n})\n@EnableConfigurationProperties(AppProperties.class)\n```\n\nThis one better approximates (and verifies) what the final application will do by loading the\nexternalized configuration from the main `application.yml`. The addition of the\n[`ConfigFileApplicationContextInitializer`](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-configfileapplicationcontextinitializer-test-utility) \nin the `@ContextConfiguration` is what activated that behavior.\n\n## Minimal context using test specific properties file\n\n```java\n@RunWith(SpringRunner.class)\n@ContextConfiguration(classes = {\n    UsesAppPropertiesService.class\n})\n@EnableConfigurationProperties(AppProperties.class)\n@TestPropertySource\n```\n\nThis one uses a feature provided by Spring Core's test facilities, which is the\n[`@TestPropertySource`](https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#spring-testing-annotation-testpropertysource).\nThis default use of the annotation is especially useful since you can alter the properties used for\neach test suite. It looks for a `.properties` file named for the test class and the package/directory\ncontaining it. In this repository, that file is `me/itzg/trytestpropertiesloading/MinimalContextPropSourceTest.properties`,\nwhich is located in `src/test/resources`.\n\n## Minimal context using inline property values \n\n```java\n@RunWith(SpringRunner.class)\n@ContextConfiguration(classes = {\n    UsesAppPropertiesService.class\n})\n@EnableConfigurationProperties(AppProperties.class)\n@TestPropertySource(properties = \"app.identifier=inline\")\n```\n\nThis one is a slight variation on the previous, replacing the properties file with the\nproperties set inline using the `properties` of `@TestPropertySource`. Note how this approach is\nconsistent with the `properties` of `@SpringBootTest` used above.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fitzg%2Ftry-sb-test-properties-loading","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fitzg%2Ftry-sb-test-properties-loading","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fitzg%2Ftry-sb-test-properties-loading/lists"}