diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/ValueObjectBinderTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/ValueObjectBinderTests.java index 0d39e4316be..3abd1067907 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/ValueObjectBinderTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/ValueObjectBinderTests.java @@ -369,15 +369,25 @@ class ValueObjectBinderTests { } @Test - void bindToAnnotationNamedParameter() { + void bindToAnnotationNamedConstructorParameter() { MockConfigurationPropertySource source = new MockConfigurationPropertySource(); source.put("test.import", "test"); this.sources.add(source); - Bindable target = Bindable.of(NamedParameter.class); - NamedParameter bound = this.binder.bindOrCreate("test", target); + Bindable target = Bindable.of(NamedConstructorParameter.class); + NamedConstructorParameter bound = this.binder.bindOrCreate("test", target); assertThat(bound.getImportName()).isEqualTo("test"); } + @Test + void bindToAnnotationNamedRecordComponent() { + MockConfigurationPropertySource source = new MockConfigurationPropertySource(); + source.put("test.import", "test"); + this.sources.add(source); + Bindable target = Bindable.of(NamedRecordComponent.class); + NamedRecordComponent bound = this.binder.bindOrCreate("test", target); + assertThat(bound.importName()).isEqualTo("test"); + } + @Test void bindToRecordWithDefaultValue() { MockConfigurationPropertySource source = new MockConfigurationPropertySource(); @@ -886,11 +896,11 @@ class ValueObjectBinderTests { } - static class NamedParameter { + static class NamedConstructorParameter { private final String importName; - NamedParameter(@Name("import") String importName) { + NamedConstructorParameter(@Name("import") String importName) { this.importName = importName; } @@ -900,6 +910,9 @@ class ValueObjectBinderTests { } + record NamedRecordComponent(@Name("import") String importName) { + } + static class NonExtractableParameterName { private String value; diff --git a/spring-boot-project/spring-boot/src/test/kotlin/org/springframework/boot/context/properties/bind/KotlinConstructorParametersBinderTests.kt b/spring-boot-project/spring-boot/src/test/kotlin/org/springframework/boot/context/properties/bind/KotlinConstructorParametersBinderTests.kt index 6b908843b63..19d178594f6 100644 --- a/spring-boot-project/spring-boot/src/test/kotlin/org/springframework/boot/context/properties/bind/KotlinConstructorParametersBinderTests.kt +++ b/spring-boot-project/spring-boot/src/test/kotlin/org/springframework/boot/context/properties/bind/KotlinConstructorParametersBinderTests.kt @@ -179,10 +179,18 @@ class KotlinConstructorParametersBinderTests { } @Test - fun `Bind to named constructor parameter`() { + fun `Bind to named data class constructor parameter`() { val source = MockConfigurationPropertySource("foo.string-value", "test") val binder = Binder(source) - val bean = binder.bind("foo", Bindable.of(ExampleNamedParameterBean::class.java)).get() + val bean = binder.bind("foo", Bindable.of(ExampleNamedParameterDataClass::class.java)).get() + assertThat(bean.stringDataValue).isEqualTo("test") + } + + @Test + fun `Bind to named class constructor parameter`() { + val source = MockConfigurationPropertySource("foo.string-value", "test") + val binder = Binder(source) + val bean = binder.bind("foo", Bindable.of(ExampleNamedParameterClass::class.java)).get() assertThat(bean.stringDataValue).isEqualTo("test") } @@ -235,7 +243,9 @@ class KotlinConstructorParametersBinderTests { val stringValue: String = "my data", val enumValue: ExampleEnum = ExampleEnum.BAR_BAZ) - data class ExampleNamedParameterBean(@Name("stringValue") val stringDataValue: String) + data class ExampleNamedParameterDataClass(@Name("stringValue") val stringDataValue: String) + + class ExampleNamedParameterClass(@Name("stringValue") val stringDataValue: String) data class GenericValue( val value: T