|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2024 the original author or authors. |
| 2 | + * Copyright 2012-2025 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
36 | 36 | import org.springframework.core.DefaultParameterNameDiscoverer;
|
37 | 37 | import org.springframework.core.ResolvableType;
|
38 | 38 | import org.springframework.core.convert.ConversionService;
|
39 |
| -import org.springframework.core.test.tools.SourceFile; |
40 |
| -import org.springframework.core.test.tools.TestCompiler; |
41 | 39 | import org.springframework.format.annotation.DateTimeFormat;
|
42 | 40 | import org.springframework.util.Assert;
|
43 | 41 |
|
44 | 42 | import static org.assertj.core.api.Assertions.assertThat;
|
45 | 43 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
46 |
| -import static org.assertj.core.api.Assertions.fail; |
47 | 44 |
|
48 | 45 | /**
|
49 | 46 | * Tests for {@link ValueObjectBinder}.
|
50 | 47 | *
|
51 | 48 | * @author Madhura Bhave
|
52 | 49 | * @author Phillip Webb
|
53 | 50 | * @author Pavel Anisimov
|
| 51 | + * @author Yanming Zhou |
54 | 52 | */
|
55 | 53 | class ValueObjectBinderTests {
|
56 | 54 |
|
@@ -390,25 +388,12 @@ void bindToAnnotationNamedRecordComponent() {
|
390 | 388 | @Test
|
391 | 389 | void bindToRecordWithDefaultValue() {
|
392 | 390 | MockConfigurationPropertySource source = new MockConfigurationPropertySource();
|
393 |
| - source.put("test.record.property1", "value-from-config-1"); |
| 391 | + source.put("test.property1", "value-from-config-1"); |
394 | 392 | this.sources.add(source);
|
395 |
| - String recordProperties = """ |
396 |
| - public record RecordProperties( |
397 |
| - @org.springframework.boot.context.properties.bind.DefaultValue("default-value-1") String property1, |
398 |
| - @org.springframework.boot.context.properties.bind.DefaultValue("default-value-2") String property2) { |
399 |
| - } |
400 |
| - """; |
401 |
| - TestCompiler.forSystem().withSources(SourceFile.of(recordProperties)).compile((compiled) -> { |
402 |
| - try { |
403 |
| - ClassLoader cl = compiled.getClassLoader(); |
404 |
| - Object bean = this.binder.bind("test.record", Class.forName("RecordProperties", true, cl)).get(); |
405 |
| - assertThat(bean).hasFieldOrPropertyWithValue("property1", "value-from-config-1") |
406 |
| - .hasFieldOrPropertyWithValue("property2", "default-value-2"); |
407 |
| - } |
408 |
| - catch (ClassNotFoundException ex) { |
409 |
| - fail("Expected generated class 'RecordProperties' not found", ex); |
410 |
| - } |
411 |
| - }); |
| 393 | + Bindable<RecordProperties> target = Bindable.of(RecordProperties.class); |
| 394 | + RecordProperties bound = this.binder.bindOrCreate("test", target); |
| 395 | + assertThat(bound.property1()).isEqualTo("value-from-config-1"); |
| 396 | + assertThat(bound.property2()).isEqualTo("default-value-2"); |
412 | 397 | }
|
413 | 398 |
|
414 | 399 | @Test // gh-38201
|
@@ -912,6 +897,10 @@ String getImportName() {
|
912 | 897 | record NamedRecordComponent(@Name("import") String importName) {
|
913 | 898 | }
|
914 | 899 |
|
| 900 | + record RecordProperties(@DefaultValue("default-value-1") String property1, |
| 901 | + @DefaultValue("default-value-2") String property2) { |
| 902 | + } |
| 903 | + |
915 | 904 | static class NonExtractableParameterName {
|
916 | 905 |
|
917 | 906 | private String value;
|
|
0 commit comments