Skip to content

Commit cad7ef2

Browse files
committed
Polishing tests
1. Improve null safety 2. Replace lambda with method reference
1 parent dc78bd4 commit cad7ef2

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ void getWhenHasFactoryMethodAndBeanAnnotationFavorsFactoryMethod() throws Throwa
197197
void getWhenHasValidatedBeanBindsWithBeanAnnotation() throws Throwable {
198198
get(ValidatedBeanConfiguration.class, "validatedBean", (propertiesBean) -> {
199199
Validated validated = propertiesBean.asBindTarget().getAnnotation(Validated.class);
200+
assertThat(validated).isNotNull();
200201
assertThat(validated.value()).containsExactly(BeanGroup.class);
201202
});
202203
}
@@ -205,6 +206,7 @@ void getWhenHasValidatedBeanBindsWithBeanAnnotation() throws Throwable {
205206
void getWhenHasValidatedFactoryMethodBindsWithFactoryMethodAnnotation() throws Throwable {
206207
get(ValidatedMethodConfiguration.class, "annotatedBean", (propertiesBean) -> {
207208
Validated validated = propertiesBean.asBindTarget().getAnnotation(Validated.class);
209+
assertThat(validated).isNotNull();
208210
assertThat(validated.value()).containsExactly(FactoryMethodGroup.class);
209211
});
210212
}
@@ -213,6 +215,7 @@ void getWhenHasValidatedFactoryMethodBindsWithFactoryMethodAnnotation() throws T
213215
void getWhenHasValidatedBeanAndFactoryMethodBindsWithFactoryMethodAnnotation() throws Throwable {
214216
get(ValidatedMethodAndBeanConfiguration.class, "validatedBean", (propertiesBean) -> {
215217
Validated validated = propertiesBean.asBindTarget().getAnnotation(Validated.class);
218+
assertThat(validated).isNotNull();
216219
assertThat(validated.value()).containsExactly(FactoryMethodGroup.class);
217220
});
218221
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindExceptionTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ void createFromBeanHasDetails() {
3636
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Example.class);
3737
ConfigurationPropertiesBean bean = ConfigurationPropertiesBean.get(applicationContext,
3838
applicationContext.getBean(Example.class), "example");
39+
assertThat(bean).isNotNull();
3940
ConfigurationPropertiesBindException exception = new ConfigurationPropertiesBindException(bean,
4041
new IllegalStateException());
4142
assertThat(exception.getMessage()).isEqualTo("Error creating bean with name 'example': "

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,7 @@ void loadWhenBoundToRandomPropertyPlaceholder() {
11971197
void boundPropertiesShouldBeRecorded() {
11981198
load(NestedConfiguration.class, "name=foo", "nested.name=bar");
11991199
BoundConfigurationProperties bound = BoundConfigurationProperties.get(this.context);
1200+
assertThat(bound).isNotNull();
12001201
Set<ConfigurationPropertyName> keys = bound.getAll().keySet();
12011202
assertThat(keys.stream().map(ConfigurationPropertyName::toString)).contains("name", "nested.name");
12021203
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/PropertyMapperTests.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818

1919
import java.util.function.Supplier;
2020

21+
import org.assertj.core.api.Assertions;
2122
import org.junit.jupiter.api.Test;
2223

2324
import static org.assertj.core.api.Assertions.assertThat;
2425
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25-
import static org.assertj.core.api.Assertions.fail;
2626

2727
/**
2828
* Tests for {@link PropertyMapper}.
@@ -57,7 +57,7 @@ void fromValueAsIntShouldAdaptValue() {
5757

5858
@Test
5959
void fromValueAlwaysApplyingWhenNonNullShouldAlwaysApplyNonNullToSource() {
60-
this.map.alwaysApplyingWhenNonNull().from((String) null).toCall(() -> fail());
60+
this.map.alwaysApplyingWhenNonNull().from((String) null).toCall(Assertions::fail);
6161
}
6262

6363
@Test
@@ -101,14 +101,14 @@ void asShouldAdaptSupplier() {
101101

102102
@Test
103103
void whenNonNullWhenSuppliedNullShouldNotMap() {
104-
this.map.from(() -> null).whenNonNull().as(String::valueOf).toCall(() -> fail());
104+
this.map.from(() -> null).whenNonNull().as(String::valueOf).toCall(Assertions::fail);
105105
}
106106

107107
@Test
108108
void whenNonNullWhenSuppliedThrowsNullPointerExceptionShouldNotMap() {
109109
this.map.from(() -> {
110110
throw new NullPointerException();
111-
}).whenNonNull().as(String::valueOf).toCall(() -> fail());
111+
}).whenNonNull().as(String::valueOf).toCall(Assertions::fail);
112112
}
113113

114114
@Test
@@ -119,7 +119,7 @@ void whenTrueWhenValueIsTrueShouldMap() {
119119

120120
@Test
121121
void whenTrueWhenValueIsFalseShouldNotMap() {
122-
this.map.from(false).whenTrue().toCall(() -> fail());
122+
this.map.from(false).whenTrue().toCall(Assertions::fail);
123123
}
124124

125125
@Test
@@ -130,17 +130,17 @@ void whenFalseWhenValueIsFalseShouldMap() {
130130

131131
@Test
132132
void whenFalseWhenValueIsTrueShouldNotMap() {
133-
this.map.from(true).whenFalse().toCall(() -> fail());
133+
this.map.from(true).whenFalse().toCall(Assertions::fail);
134134
}
135135

136136
@Test
137137
void whenHasTextWhenValueIsNullShouldNotMap() {
138-
this.map.from(() -> null).whenHasText().toCall(() -> fail());
138+
this.map.from(() -> null).whenHasText().toCall(Assertions::fail);
139139
}
140140

141141
@Test
142142
void whenHasTextWhenValueIsEmptyShouldNotMap() {
143-
this.map.from("").whenHasText().toCall(() -> fail());
143+
this.map.from("").whenHasText().toCall(Assertions::fail);
144144
}
145145

146146
@Test
@@ -157,7 +157,7 @@ void whenEqualToWhenValueIsEqualShouldMatch() {
157157

158158
@Test
159159
void whenEqualToWhenValueIsNotEqualShouldNotMatch() {
160-
this.map.from("123").whenEqualTo("321").toCall(() -> fail());
160+
this.map.from("123").whenEqualTo("321").toCall(Assertions::fail);
161161
}
162162

163163
@Test
@@ -169,7 +169,7 @@ void whenInstanceOfWhenValueIsTargetTypeShouldMatch() {
169169
@Test
170170
void whenInstanceOfWhenValueIsNotTargetTypeShouldNotMatch() {
171171
Supplier<Number> supplier = () -> 123L;
172-
this.map.from(supplier).whenInstanceOf(Double.class).toCall(() -> fail());
172+
this.map.from(supplier).whenInstanceOf(Double.class).toCall(Assertions::fail);
173173
}
174174

175175
@Test
@@ -180,7 +180,7 @@ void whenWhenValueMatchesShouldMap() {
180180

181181
@Test
182182
void whenWhenValueDoesNotMatchShouldNotMap() {
183-
this.map.from("123").when("321"::equals).toCall(() -> fail());
183+
this.map.from("123").when("321"::equals).toCall(Assertions::fail);
184184
}
185185

186186
@Test
@@ -198,12 +198,12 @@ void whenWhenCombinedWithAsUsesSourceValue() {
198198

199199
@Test
200200
void alwaysApplyingWhenNonNullShouldAlwaysApplyNonNullToSource() {
201-
this.map.alwaysApplyingWhenNonNull().from(() -> null).toCall(() -> fail());
201+
this.map.alwaysApplyingWhenNonNull().from(() -> null).toCall(Assertions::fail);
202202
}
203203

204204
@Test
205205
void whenWhenValueNotMatchesShouldSupportChainedCalls() {
206-
this.map.from("123").when("456"::equals).when("123"::equals).toCall(() -> fail());
206+
this.map.from("123").when("456"::equals).when("123"::equals).toCall(Assertions::fail);
207207
}
208208

209209
@Test

0 commit comments

Comments
 (0)