Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,19 @@
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.ResolvableType;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.test.tools.SourceFile;
import org.springframework.core.test.tools.TestCompiler;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.Assert;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.fail;

/**
* Tests for {@link ValueObjectBinder}.
*
* @author Madhura Bhave
* @author Phillip Webb
* @author Pavel Anisimov
* @author Yanming Zhou
*/
class ValueObjectBinderTests {

Expand Down Expand Up @@ -390,25 +388,12 @@ void bindToAnnotationNamedRecordComponent() {
@Test
void bindToRecordWithDefaultValue() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("test.record.property1", "value-from-config-1");
source.put("test.property1", "value-from-config-1");
this.sources.add(source);
String recordProperties = """
public record RecordProperties(
@org.springframework.boot.context.properties.bind.DefaultValue("default-value-1") String property1,
@org.springframework.boot.context.properties.bind.DefaultValue("default-value-2") String property2) {
}
""";
TestCompiler.forSystem().withSources(SourceFile.of(recordProperties)).compile((compiled) -> {
try {
ClassLoader cl = compiled.getClassLoader();
Object bean = this.binder.bind("test.record", Class.forName("RecordProperties", true, cl)).get();
assertThat(bean).hasFieldOrPropertyWithValue("property1", "value-from-config-1")
.hasFieldOrPropertyWithValue("property2", "default-value-2");
}
catch (ClassNotFoundException ex) {
fail("Expected generated class 'RecordProperties' not found", ex);
}
});
Bindable<RecordProperties> target = Bindable.of(RecordProperties.class);
RecordProperties bound = this.binder.bindOrCreate("test", target);
assertThat(bound.property1()).isEqualTo("value-from-config-1");
assertThat(bound.property2()).isEqualTo("default-value-2");
}

@Test // gh-38201
Expand Down Expand Up @@ -912,6 +897,10 @@ String getImportName() {
record NamedRecordComponent(@Name("import") String importName) {
}

record RecordProperties(@DefaultValue("default-value-1") String property1,
@DefaultValue("default-value-2") String property2) {
}

static class NonExtractableParameterName {

private String value;
Expand Down