20
20
import java .util .Map ;
21
21
22
22
import com .jayway .jsonpath .JsonPath ;
23
+ import org .hamcrest .CoreMatchers ;
23
24
import org .hamcrest .Matcher ;
25
+ import org .hamcrest .MatcherAssert ;
24
26
25
27
import org .springframework .lang .Nullable ;
26
28
import org .springframework .util .Assert ;
27
29
import org .springframework .util .ObjectUtils ;
28
30
import org .springframework .util .StringUtils ;
29
31
30
- import static org .hamcrest .MatcherAssert .assertThat ;
31
- import static org .hamcrest .core .IsInstanceOf .instanceOf ;
32
- import static org .springframework .test .util .AssertionErrors .assertEquals ;
33
- import static org .springframework .test .util .AssertionErrors .assertTrue ;
34
- import static org .springframework .test .util .AssertionErrors .fail ;
35
-
36
32
/**
37
33
* A helper class for applying assertions via JSON path expressions.
38
34
*
@@ -74,7 +70,7 @@ public JsonPathExpectationsHelper(String expression, Object... args) {
74
70
@ SuppressWarnings ("unchecked" )
75
71
public <T > void assertValue (String content , Matcher <T > matcher ) {
76
72
T value = (T ) evaluateJsonPath (content );
77
- assertThat ("JSON path \" " + this .expression + "\" " , value , matcher );
73
+ MatcherAssert . assertThat ("JSON path \" " + this .expression + "\" " , value , matcher );
78
74
}
79
75
80
76
/**
@@ -89,7 +85,7 @@ public <T> void assertValue(String content, Matcher<T> matcher) {
89
85
@ SuppressWarnings ("unchecked" )
90
86
public <T > void assertValue (String content , Matcher <T > matcher , Class <T > targetType ) {
91
87
T value = (T ) evaluateJsonPath (content , targetType );
92
- assertThat ("JSON path \" " + this .expression + "\" " , value , matcher );
88
+ MatcherAssert . assertThat ("JSON path \" " + this .expression + "\" " , value , matcher );
93
89
}
94
90
95
91
/**
@@ -104,10 +100,11 @@ public void assertValue(String content, @Nullable Object expectedValue) {
104
100
@ SuppressWarnings ("rawtypes" )
105
101
List actualValueList = (List ) actualValue ;
106
102
if (actualValueList .isEmpty ()) {
107
- fail ("No matching value at JSON path \" " + this .expression + "\" " );
103
+ AssertionErrors . fail ("No matching value at JSON path \" " + this .expression + "\" " );
108
104
}
109
105
if (actualValueList .size () != 1 ) {
110
- fail ("Got a list of values " + actualValue + " instead of the expected single value " + expectedValue );
106
+ AssertionErrors .fail ("Got a list of values " + actualValue +
107
+ " instead of the expected single value " + expectedValue );
111
108
}
112
109
actualValue = actualValueList .get (0 );
113
110
}
@@ -116,7 +113,7 @@ else if (actualValue != null && expectedValue != null) {
116
113
actualValue = evaluateJsonPath (content , expectedValue .getClass ());
117
114
}
118
115
}
119
- assertEquals ("JSON path \" " + this .expression + "\" " , expectedValue , actualValue );
116
+ AssertionErrors . assertEquals ("JSON path \" " + this .expression + "\" " , expectedValue , actualValue );
120
117
}
121
118
122
119
/**
@@ -127,7 +124,7 @@ else if (actualValue != null && expectedValue != null) {
127
124
*/
128
125
public void assertValueIsString (String content ) {
129
126
Object value = assertExistsAndReturn (content );
130
- assertThat (failureReason ("a string" , value ), value , instanceOf (String .class ));
127
+ MatcherAssert . assertThat (failureReason ("a string" , value ), value , CoreMatchers . instanceOf (String .class ));
131
128
}
132
129
133
130
/**
@@ -138,7 +135,7 @@ public void assertValueIsString(String content) {
138
135
*/
139
136
public void assertValueIsBoolean (String content ) {
140
137
Object value = assertExistsAndReturn (content );
141
- assertThat (failureReason ("a boolean" , value ), value , instanceOf (Boolean .class ));
138
+ MatcherAssert . assertThat (failureReason ("a boolean" , value ), value , CoreMatchers . instanceOf (Boolean .class ));
142
139
}
143
140
144
141
/**
@@ -149,7 +146,7 @@ public void assertValueIsBoolean(String content) {
149
146
*/
150
147
public void assertValueIsNumber (String content ) {
151
148
Object value = assertExistsAndReturn (content );
152
- assertThat (failureReason ("a number" , value ), value , instanceOf (Number .class ));
149
+ MatcherAssert . assertThat (failureReason ("a number" , value ), value , CoreMatchers . instanceOf (Number .class ));
153
150
}
154
151
155
152
/**
@@ -159,7 +156,7 @@ public void assertValueIsNumber(String content) {
159
156
*/
160
157
public void assertValueIsArray (String content ) {
161
158
Object value = assertExistsAndReturn (content );
162
- assertThat (failureReason ("an array" , value ), value , instanceOf (List .class ));
159
+ MatcherAssert . assertThat (failureReason ("an array" , value ), value , CoreMatchers . instanceOf (List .class ));
163
160
}
164
161
165
162
/**
@@ -170,7 +167,7 @@ public void assertValueIsArray(String content) {
170
167
*/
171
168
public void assertValueIsMap (String content ) {
172
169
Object value = assertExistsAndReturn (content );
173
- assertThat (failureReason ("a map" , value ), value , instanceOf (Map .class ));
170
+ MatcherAssert . assertThat (failureReason ("a map" , value ), value , CoreMatchers . instanceOf (Map .class ));
174
171
}
175
172
176
173
/**
@@ -204,10 +201,10 @@ public void doesNotExist(String content) {
204
201
}
205
202
String reason = failureReason ("no value" , value );
206
203
if (pathIsIndefinite () && value instanceof List ) {
207
- assertTrue (reason , ((List <?>) value ).isEmpty ());
204
+ AssertionErrors . assertTrue (reason , ((List <?>) value ).isEmpty ());
208
205
}
209
206
else {
210
- assertTrue (reason , (value == null ));
207
+ AssertionErrors . assertTrue (reason , (value == null ));
211
208
}
212
209
}
213
210
@@ -220,7 +217,7 @@ public void doesNotExist(String content) {
220
217
*/
221
218
public void assertValueIsEmpty (String content ) {
222
219
Object value = evaluateJsonPath (content );
223
- assertTrue (failureReason ("an empty value" , value ), ObjectUtils .isEmpty (value ));
220
+ AssertionErrors . assertTrue (failureReason ("an empty value" , value ), ObjectUtils .isEmpty (value ));
224
221
}
225
222
226
223
/**
@@ -232,7 +229,7 @@ public void assertValueIsEmpty(String content) {
232
229
*/
233
230
public void assertValueIsNotEmpty (String content ) {
234
231
Object value = evaluateJsonPath (content );
235
- assertTrue (failureReason ("a non-empty value" , value ), !ObjectUtils .isEmpty (value ));
232
+ AssertionErrors . assertTrue (failureReason ("a non-empty value" , value ), !ObjectUtils .isEmpty (value ));
236
233
}
237
234
238
235
/**
@@ -247,7 +244,8 @@ public void assertValueIsNotEmpty(String content) {
247
244
public void hasJsonPath (String content ) {
248
245
Object value = evaluateJsonPath (content );
249
246
if (pathIsIndefinite () && value instanceof List ) {
250
- assertTrue ("No values for JSON path \" " + this .expression + "\" " , !((List <?>) value ).isEmpty ());
247
+ String message = "No values for JSON path \" " + this .expression + "\" " ;
248
+ AssertionErrors .assertTrue (message , !((List <?>) value ).isEmpty ());
251
249
}
252
250
}
253
251
@@ -270,10 +268,10 @@ public void doesNotHaveJsonPath(String content) {
270
268
return ;
271
269
}
272
270
if (pathIsIndefinite () && value instanceof List ) {
273
- assertTrue (failureReason ("no values" , value ), ((List <?>) value ).isEmpty ());
271
+ AssertionErrors . assertTrue (failureReason ("no values" , value ), ((List <?>) value ).isEmpty ());
274
272
}
275
273
else {
276
- fail (failureReason ("no value" , value ));
274
+ AssertionErrors . fail (failureReason ("no value" , value ));
277
275
}
278
276
}
279
277
@@ -282,18 +280,31 @@ private String failureReason(String expectedDescription, @Nullable Object value)
282
280
ObjectUtils .nullSafeToString (StringUtils .quoteIfString (value )));
283
281
}
284
282
283
+ /**
284
+ * Evaluate the JSON path and return the resulting value.
285
+ * @param content the content to evaluate against
286
+ * @return the result of the evaluation
287
+ * @throws AssertionError if the evaluation fails
288
+ */
285
289
@ Nullable
286
- private Object evaluateJsonPath (String content ) {
290
+ public Object evaluateJsonPath (String content ) {
287
291
try {
288
292
return this .jsonPath .read (content );
289
293
}
290
294
catch (Throwable ex ) {
291
- String message = "No value at JSON path \" " + this .expression + "\" " ;
292
- throw new AssertionError (message , ex );
295
+ throw new AssertionError ("No value at JSON path \" " + this .expression + "\" " , ex );
293
296
}
294
297
}
295
298
296
- private Object evaluateJsonPath (String content , Class <?> targetType ) {
299
+ /**
300
+ * Variant of {@link #evaluateJsonPath(String)} with a target type.
301
+ * This can be useful for matching numbers reliably for example coercing an
302
+ * integer into a double.
303
+ * @param content the content to evaluate against
304
+ * @return the result of the evaluation
305
+ * @throws AssertionError if the evaluation fails
306
+ */
307
+ public Object evaluateJsonPath (String content , Class <?> targetType ) {
297
308
try {
298
309
return JsonPath .parse (content ).read (this .expression , targetType );
299
310
}
@@ -307,9 +318,9 @@ private Object evaluateJsonPath(String content, Class<?> targetType) {
307
318
private Object assertExistsAndReturn (String content ) {
308
319
Object value = evaluateJsonPath (content );
309
320
String reason = "No value at JSON path \" " + this .expression + "\" " ;
310
- assertTrue (reason , value != null );
321
+ AssertionErrors . assertTrue (reason , value != null );
311
322
if (pathIsIndefinite () && value instanceof List ) {
312
- assertTrue (reason , !((List <?>) value ).isEmpty ());
323
+ AssertionErrors . assertTrue (reason , !((List <?>) value ).isEmpty ());
313
324
}
314
325
return value ;
315
326
}
0 commit comments