21
21
import java .util .List ;
22
22
import java .util .Map ;
23
23
24
+ import org .assertj .core .api .ThrowableTypeAssert ;
24
25
import org .junit .jupiter .api .Test ;
25
26
26
27
import org .springframework .core .convert .TypeDescriptor ;
@@ -83,11 +84,11 @@ void nonExistentPropertiesAndMethods() {
83
84
void accessingOnNullObject () {
84
85
SpelExpression expr = (SpelExpression ) parser .parseExpression ("madeup" );
85
86
EvaluationContext context = new StandardEvaluationContext (null );
86
- assertThatExceptionOfType ( SpelEvaluationException . class )
87
+ assertThatSpelEvaluationException ( )
87
88
.isThrownBy (() -> expr .getValue (context ))
88
89
.extracting (SpelEvaluationException ::getMessageCode ).isEqualTo (SpelMessage .PROPERTY_OR_FIELD_NOT_READABLE_ON_NULL );
89
90
assertThat (expr .isWritable (context )).isFalse ();
90
- assertThatExceptionOfType ( SpelEvaluationException . class )
91
+ assertThatSpelEvaluationException ( )
91
92
.isThrownBy (() -> expr .setValue (context , "abc" ))
92
93
.extracting (SpelEvaluationException ::getMessageCode ).isEqualTo (SpelMessage .PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL );
93
94
}
@@ -117,8 +118,7 @@ void addingSpecificPropertyAccessor() {
117
118
assertThat ((int ) i ).isEqualTo (99 );
118
119
119
120
// 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" ));
122
122
// message will be: EL1063E:(pos 20): A problem occurred whilst attempting to set the property
123
123
// 'flibbles': 'Cannot set flibbles to an object of type 'class java.lang.String''
124
124
// System.out.println(e.getMessage());
@@ -173,8 +173,7 @@ void standardGetClassAccess() {
173
173
@ Test
174
174
void noGetClassAccess () {
175
175
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 ));
178
177
}
179
178
180
179
@ Test
@@ -187,8 +186,9 @@ void propertyReadOnly() {
187
186
target .setName ("p2" );
188
187
assertThat (expr .getValue (context , target )).isEqualTo ("p2" );
189
188
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 );
192
192
}
193
193
194
194
@ Test
@@ -201,8 +201,9 @@ void propertyReadOnlyWithRecordStyle() {
201
201
RecordPerson target2 = new RecordPerson ("p2" );
202
202
assertThat (expr .getValue (context , target2 )).isEqualTo ("p2" );
203
203
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 );
206
207
}
207
208
208
209
@ Test
@@ -248,7 +249,7 @@ void propertyReadWriteWithRootObject() {
248
249
void propertyAccessWithoutMethodResolver () {
249
250
EvaluationContext context = SimpleEvaluationContext .forReadOnlyDataBinding ().build ();
250
251
Person target = new Person ("p1" );
251
- assertThatExceptionOfType ( SpelEvaluationException . class ).isThrownBy (() ->
252
+ assertThatSpelEvaluationException ( ).isThrownBy (() ->
252
253
parser .parseExpression ("name.substring(1)" ).getValue (context , target ));
253
254
}
254
255
@@ -274,12 +275,17 @@ void propertyAccessWithInstanceMethodResolverAndTypedRootObject() {
274
275
void propertyAccessWithArrayIndexOutOfBounds () {
275
276
EvaluationContext context = SimpleEvaluationContext .forReadOnlyDataBinding ().build ();
276
277
Expression expression = parser .parseExpression ("stringArrayOfThreeItems[3]" );
277
- assertThatExceptionOfType ( SpelEvaluationException . class )
278
+ assertThatSpelEvaluationException ( )
278
279
.isThrownBy (() -> expression .getValue (context , new Inventor ()))
279
280
.extracting (SpelEvaluationException ::getMessageCode ).isEqualTo (SpelMessage .ARRAY_INDEX_OUT_OF_BOUNDS );
280
281
}
281
282
282
283
284
+ private ThrowableTypeAssert <SpelEvaluationException > assertThatSpelEvaluationException () {
285
+ return assertThatExceptionOfType (SpelEvaluationException .class );
286
+ }
287
+
288
+
283
289
// This can resolve the property 'flibbles' on any String (very useful...)
284
290
private static class StringyPropertyAccessor implements PropertyAccessor {
285
291
0 commit comments