Skip to content

Commit bf72e92

Browse files
committed
Polishing
1 parent 3e73724 commit bf72e92

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

spring-expression/src/test/java/org/springframework/expression/spel/PropertyAccessTests.java

+18-12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.List;
2222
import java.util.Map;
2323

24+
import org.assertj.core.api.ThrowableTypeAssert;
2425
import org.junit.jupiter.api.Test;
2526

2627
import org.springframework.core.convert.TypeDescriptor;
@@ -83,11 +84,11 @@ void nonExistentPropertiesAndMethods() {
8384
void accessingOnNullObject() {
8485
SpelExpression expr = (SpelExpression) parser.parseExpression("madeup");
8586
EvaluationContext context = new StandardEvaluationContext(null);
86-
assertThatExceptionOfType(SpelEvaluationException.class)
87+
assertThatSpelEvaluationException()
8788
.isThrownBy(() -> expr.getValue(context))
8889
.extracting(SpelEvaluationException::getMessageCode).isEqualTo(SpelMessage.PROPERTY_OR_FIELD_NOT_READABLE_ON_NULL);
8990
assertThat(expr.isWritable(context)).isFalse();
90-
assertThatExceptionOfType(SpelEvaluationException.class)
91+
assertThatSpelEvaluationException()
9192
.isThrownBy(() -> expr.setValue(context, "abc"))
9293
.extracting(SpelEvaluationException::getMessageCode).isEqualTo(SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL);
9394
}
@@ -117,8 +118,7 @@ void addingSpecificPropertyAccessor() {
117118
assertThat((int) i).isEqualTo(99);
118119

119120
// Cannot set it to a string value
120-
assertThatExceptionOfType(EvaluationException.class).isThrownBy(() ->
121-
flibbleexpr.setValue(ctx, "not allowed"));
121+
assertThatSpelEvaluationException().isThrownBy(() -> flibbleexpr.setValue(ctx, "not allowed"));
122122
// message will be: EL1063E:(pos 20): A problem occurred whilst attempting to set the property
123123
// 'flibbles': 'Cannot set flibbles to an object of type 'class java.lang.String''
124124
// System.out.println(e.getMessage());
@@ -173,8 +173,7 @@ void standardGetClassAccess() {
173173
@Test
174174
void noGetClassAccess() {
175175
EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
176-
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
177-
parser.parseExpression("'a'.class.name").getValue(context));
176+
assertThatSpelEvaluationException().isThrownBy(() -> parser.parseExpression("'a'.class.name").getValue(context));
178177
}
179178

180179
@Test
@@ -187,8 +186,9 @@ void propertyReadOnly() {
187186
target.setName("p2");
188187
assertThat(expr.getValue(context, target)).isEqualTo("p2");
189188

190-
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
191-
parser.parseExpression("name='p3'").getValue(context, target));
189+
assertThatSpelEvaluationException()
190+
.isThrownBy(() -> parser.parseExpression("name='p3'").getValue(context, target))
191+
.extracting(SpelEvaluationException::getMessageCode).isEqualTo(SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE);
192192
}
193193

194194
@Test
@@ -201,8 +201,9 @@ void propertyReadOnlyWithRecordStyle() {
201201
RecordPerson target2 = new RecordPerson("p2");
202202
assertThat(expr.getValue(context, target2)).isEqualTo("p2");
203203

204-
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
205-
parser.parseExpression("name='p3'").getValue(context, target2));
204+
assertThatSpelEvaluationException()
205+
.isThrownBy(() -> parser.parseExpression("name='p3'").getValue(context, target2))
206+
.extracting(SpelEvaluationException::getMessageCode).isEqualTo(SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE);
206207
}
207208

208209
@Test
@@ -248,7 +249,7 @@ void propertyReadWriteWithRootObject() {
248249
void propertyAccessWithoutMethodResolver() {
249250
EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
250251
Person target = new Person("p1");
251-
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
252+
assertThatSpelEvaluationException().isThrownBy(() ->
252253
parser.parseExpression("name.substring(1)").getValue(context, target));
253254
}
254255

@@ -274,12 +275,17 @@ void propertyAccessWithInstanceMethodResolverAndTypedRootObject() {
274275
void propertyAccessWithArrayIndexOutOfBounds() {
275276
EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
276277
Expression expression = parser.parseExpression("stringArrayOfThreeItems[3]");
277-
assertThatExceptionOfType(SpelEvaluationException.class)
278+
assertThatSpelEvaluationException()
278279
.isThrownBy(() -> expression.getValue(context, new Inventor()))
279280
.extracting(SpelEvaluationException::getMessageCode).isEqualTo(SpelMessage.ARRAY_INDEX_OUT_OF_BOUNDS);
280281
}
281282

282283

284+
private ThrowableTypeAssert<SpelEvaluationException> assertThatSpelEvaluationException() {
285+
return assertThatExceptionOfType(SpelEvaluationException.class);
286+
}
287+
288+
283289
// This can resolve the property 'flibbles' on any String (very useful...)
284290
private static class StringyPropertyAccessor implements PropertyAccessor {
285291

0 commit comments

Comments
 (0)