|
22 | 22 |
|
23 | 23 | import org.junit.jupiter.api.Test;
|
24 | 24 |
|
| 25 | +import org.springframework.core.convert.ConversionFailedException; |
| 26 | +import org.springframework.core.convert.TypeDescriptor; |
25 | 27 | import org.springframework.core.env.ConfigurablePropertyResolver;
|
26 | 28 | import org.springframework.core.env.MutablePropertySources;
|
27 | 29 | import org.springframework.core.env.StandardEnvironment;
|
28 | 30 | import org.springframework.mock.env.MockPropertySource;
|
29 | 31 |
|
30 | 32 | import static org.assertj.core.api.Assertions.assertThat;
|
| 33 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
31 | 34 |
|
32 | 35 | /**
|
33 | 36 | * Tests for {@link ConfigurationPropertySourcesPropertyResolver}.
|
@@ -113,6 +116,30 @@ void getPropertyAsTypeWhenHasPlaceholder() {
|
113 | 116 | assertThat(environment.getProperty("v2", Integer.class)).isOne();
|
114 | 117 | }
|
115 | 118 |
|
| 119 | + @Test |
| 120 | + void throwsInvalidConfigurationPropertyValueExceptionWhenGetPropertyAsTypeFailsToConvert() { |
| 121 | + ResolverEnvironment environment = new ResolverEnvironment(); |
| 122 | + MockPropertySource propertySource = new MockPropertySource(); |
| 123 | + propertySource.withProperty("v1", "one"); |
| 124 | + propertySource.withProperty("v2", "${v1}"); |
| 125 | + environment.getPropertySources().addFirst(propertySource); |
| 126 | + assertThat(environment.getProperty("v2")).isEqualTo("one"); |
| 127 | + assertThatExceptionOfType(ConversionFailedException.class) |
| 128 | + .isThrownBy(() -> environment.getProperty("v2", Integer.class)) |
| 129 | + .satisfies((ex) -> { |
| 130 | + assertThat(ex.getValue()).isEqualTo("one"); |
| 131 | + assertThat(ex.getSourceType()).isEqualTo(TypeDescriptor.valueOf(String.class)); |
| 132 | + assertThat(ex.getTargetType()).isEqualTo(TypeDescriptor.valueOf(Integer.class)); |
| 133 | + }) |
| 134 | + .havingCause() |
| 135 | + .satisfies((ex) -> { |
| 136 | + InvalidConfigurationPropertyValueException invalidValueEx = (InvalidConfigurationPropertyValueException) ex; |
| 137 | + assertThat(invalidValueEx.getName()).isEqualTo("v2"); |
| 138 | + assertThat(invalidValueEx.getValue()).isEqualTo("one"); |
| 139 | + assertThat(ex).cause().isInstanceOf(NumberFormatException.class); |
| 140 | + }); |
| 141 | + } |
| 142 | + |
116 | 143 | private CountingMockPropertySource createMockPropertySource(StandardEnvironment environment, boolean attach) {
|
117 | 144 | CountingMockPropertySource propertySource = new CountingMockPropertySource();
|
118 | 145 | propertySource.withProperty("spring", "boot");
|
|
0 commit comments