17
17
package org .springframework .test .util ;
18
18
19
19
import org .junit .jupiter .api .Test ;
20
+ import org .junit .jupiter .params .ParameterizedTest ;
21
+ import org .junit .jupiter .params .provider .ValueSource ;
20
22
21
23
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
22
24
import static org .hamcrest .core .Is .is ;
@@ -55,19 +57,10 @@ class JsonPathExpectationsHelperTests {
55
57
}""" ;
56
58
57
59
58
- @ Test
59
- void exists () {
60
- new JsonPathExpectationsHelper ("$.str" ).exists (CONTENT );
61
- }
62
-
63
- @ Test
64
- void existsForAnEmptyArray () {
65
- new JsonPathExpectationsHelper ("$.emptyArray" ).exists (CONTENT );
66
- }
67
-
68
- @ Test
69
- void existsForAnEmptyMap () {
70
- new JsonPathExpectationsHelper ("$.emptyMap" ).exists (CONTENT );
60
+ @ ParameterizedTest
61
+ @ ValueSource (strings = { "$.str" , "$.emptyArray" , "$.emptyMap" })
62
+ void exists (String expression ) {
63
+ new JsonPathExpectationsHelper (expression ).exists (CONTENT );
71
64
}
72
65
73
66
@ Test
@@ -79,57 +72,49 @@ void existsForIndefinitePathWithResults() {
79
72
void existsForIndefinitePathWithEmptyResults () {
80
73
String expression = "$.familyMembers[?(@.name == 'Dilbert')]" ;
81
74
assertThatExceptionOfType (AssertionError .class ).isThrownBy (() ->
82
- new JsonPathExpectationsHelper (expression ).exists (SIMPSONS ))
83
- .withMessageContaining ("No value at JSON path \" " + expression + "\" " );
75
+ new JsonPathExpectationsHelper (expression ).exists (SIMPSONS ))
76
+ .withMessageContaining ("No value at JSON path \" " + expression + "\" " );
84
77
}
85
78
86
- @ Test
87
- void doesNotExist () {
88
- new JsonPathExpectationsHelper ("$.bogus" ).doesNotExist (CONTENT );
79
+ @ ParameterizedTest
80
+ @ ValueSource (strings = { "$.bogus" })
81
+ void doesNotExist (String expression ) {
82
+ new JsonPathExpectationsHelper (expression ).doesNotExist (CONTENT );
89
83
}
90
84
91
85
@ Test
92
86
void doesNotExistForAnEmptyArray () {
93
87
String expression = "$.emptyArray" ;
94
88
assertThatExceptionOfType (AssertionError .class ).isThrownBy (() ->
95
- new JsonPathExpectationsHelper (expression ).doesNotExist (CONTENT ))
96
- .withMessageContaining ("Expected no value at JSON path \" " + expression + "\" but found: []" );
89
+ new JsonPathExpectationsHelper (expression ).doesNotExist (CONTENT ))
90
+ .withMessageContaining ("Expected no value at JSON path \" " + expression + "\" but found: []" );
97
91
}
98
92
99
93
@ Test
100
94
void doesNotExistForAnEmptyMap () {
101
95
String expression = "$.emptyMap" ;
102
96
assertThatExceptionOfType (AssertionError .class ).isThrownBy (() ->
103
- new JsonPathExpectationsHelper (expression ).doesNotExist (CONTENT ))
104
- .withMessageContaining ("Expected no value at JSON path \" " + expression + "\" but found: {}" );
97
+ new JsonPathExpectationsHelper (expression ).doesNotExist (CONTENT ))
98
+ .withMessageContaining ("Expected no value at JSON path \" " + expression + "\" but found: {}" );
105
99
}
106
100
107
101
@ Test
108
102
void doesNotExistForIndefinitePathWithResults () {
109
103
String expression = "$.familyMembers[?(@.name == 'Bart')]" ;
110
104
assertThatExceptionOfType (AssertionError .class ).isThrownBy (() ->
111
- new JsonPathExpectationsHelper (expression ).doesNotExist (SIMPSONS ))
112
- .withMessageContaining ("Expected no value at JSON path \" " + expression + "\" but found: [{\" name\" :\" Bart\" }]" );
105
+ new JsonPathExpectationsHelper (expression ).doesNotExist (SIMPSONS ))
106
+ .withMessageContaining ("Expected no value at JSON path \" " + expression + "\" but found: [{\" name\" :\" Bart\" }]" );
113
107
}
114
108
115
109
@ Test
116
110
void doesNotExistForIndefinitePathWithEmptyResults () {
117
111
new JsonPathExpectationsHelper ("$.familyMembers[?(@.name == 'Dilbert')]" ).doesNotExist (SIMPSONS );
118
112
}
119
113
120
- @ Test
121
- void assertValueIsEmptyForAnEmptyString () {
122
- new JsonPathExpectationsHelper ("$.emptyString" ).assertValueIsEmpty (CONTENT );
123
- }
124
-
125
- @ Test
126
- void assertValueIsEmptyForAnEmptyArray () {
127
- new JsonPathExpectationsHelper ("$.emptyArray" ).assertValueIsEmpty (CONTENT );
128
- }
129
-
130
- @ Test
131
- void assertValueIsEmptyForAnEmptyMap () {
132
- new JsonPathExpectationsHelper ("$.emptyMap" ).assertValueIsEmpty (CONTENT );
114
+ @ ParameterizedTest
115
+ @ ValueSource (strings = { "$.emptyString" , "$.emptyArray" , "$.emptyMap" })
116
+ void valueIsEmpty (String expression ) {
117
+ new JsonPathExpectationsHelper (expression ).assertValueIsEmpty (CONTENT );
133
118
}
134
119
135
120
@ Test
@@ -141,41 +126,23 @@ void assertValueIsEmptyForIndefinitePathWithEmptyResults() {
141
126
void assertValueIsEmptyForIndefinitePathWithResults () {
142
127
String expression = "$.familyMembers[?(@.name == 'Bart')]" ;
143
128
assertThatExceptionOfType (AssertionError .class ).isThrownBy (() ->
144
- new JsonPathExpectationsHelper (expression ).assertValueIsEmpty (SIMPSONS ))
145
- .withMessageContaining ("Expected an empty value at JSON path \" " + expression + "\" but found: [{\" name\" :\" Bart\" }]" );
129
+ new JsonPathExpectationsHelper (expression ).assertValueIsEmpty (SIMPSONS ))
130
+ .withMessageContaining ("Expected an empty value at JSON path \" " + expression + "\" but found: [{\" name\" :\" Bart\" }]" );
146
131
}
147
132
148
133
@ Test
149
134
void assertValueIsEmptyForWhitespace () {
150
135
String expression = "$.whitespace" ;
151
136
assertThatExceptionOfType (AssertionError .class ).isThrownBy (() ->
152
- new JsonPathExpectationsHelper (expression ).assertValueIsEmpty (CONTENT ))
153
- .withMessageContaining ("Expected an empty value at JSON path \" " + expression + "\" but found: ' '" );
137
+ new JsonPathExpectationsHelper (expression ).assertValueIsEmpty (CONTENT ))
138
+ .withMessageContaining ("Expected an empty value at JSON path \" " + expression + "\" but found: ' '" );
154
139
}
155
140
156
- @ Test
157
- void assertValueIsNotEmptyForString () {
158
- new JsonPathExpectationsHelper ("$.str" ).assertValueIsNotEmpty (CONTENT );
159
- }
160
141
161
- @ Test
162
- void assertValueIsNotEmptyForNumber () {
163
- new JsonPathExpectationsHelper ("$.num" ).assertValueIsNotEmpty (CONTENT );
164
- }
165
-
166
- @ Test
167
- void assertValueIsNotEmptyForBoolean () {
168
- new JsonPathExpectationsHelper ("$.bool" ).assertValueIsNotEmpty (CONTENT );
169
- }
170
-
171
- @ Test
172
- void assertValueIsNotEmptyForArray () {
173
- new JsonPathExpectationsHelper ("$.arr" ).assertValueIsNotEmpty (CONTENT );
174
- }
175
-
176
- @ Test
177
- void assertValueIsNotEmptyForMap () {
178
- new JsonPathExpectationsHelper ("$.colorMap" ).assertValueIsNotEmpty (CONTENT );
142
+ @ ParameterizedTest
143
+ @ ValueSource (strings = { "$.str" , "$.num" , "$.bool" , "$.arr" , "$.colorMap" })
144
+ void valueIsNotEmpty (String expression ) {
145
+ new JsonPathExpectationsHelper (expression ).assertValueIsNotEmpty (CONTENT );
179
146
}
180
147
181
148
@ Test
@@ -187,32 +154,32 @@ void assertValueIsNotEmptyForIndefinitePathWithResults() {
187
154
void assertValueIsNotEmptyForIndefinitePathWithEmptyResults () {
188
155
String expression = "$.familyMembers[?(@.name == 'Dilbert')]" ;
189
156
assertThatExceptionOfType (AssertionError .class ).isThrownBy (() ->
190
- new JsonPathExpectationsHelper (expression ).assertValueIsNotEmpty (SIMPSONS ))
191
- .withMessageContaining ("Expected a non-empty value at JSON path \" " + expression + "\" but found: []" );
157
+ new JsonPathExpectationsHelper (expression ).assertValueIsNotEmpty (SIMPSONS ))
158
+ .withMessageContaining ("Expected a non-empty value at JSON path \" " + expression + "\" but found: []" );
192
159
}
193
160
194
161
@ Test
195
162
void assertValueIsNotEmptyForAnEmptyString () {
196
163
String expression = "$.emptyString" ;
197
164
assertThatExceptionOfType (AssertionError .class ).isThrownBy (() ->
198
- new JsonPathExpectationsHelper (expression ).assertValueIsNotEmpty (CONTENT ))
199
- .withMessageContaining ("Expected a non-empty value at JSON path \" " + expression + "\" but found: ''" );
165
+ new JsonPathExpectationsHelper (expression ).assertValueIsNotEmpty (CONTENT ))
166
+ .withMessageContaining ("Expected a non-empty value at JSON path \" " + expression + "\" but found: ''" );
200
167
}
201
168
202
169
@ Test
203
170
void assertValueIsNotEmptyForAnEmptyArray () {
204
171
String expression = "$.emptyArray" ;
205
172
assertThatExceptionOfType (AssertionError .class ).isThrownBy (() ->
206
- new JsonPathExpectationsHelper (expression ).assertValueIsNotEmpty (CONTENT ))
207
- .withMessageContaining ("Expected a non-empty value at JSON path \" " + expression + "\" but found: []" );
173
+ new JsonPathExpectationsHelper (expression ).assertValueIsNotEmpty (CONTENT ))
174
+ .withMessageContaining ("Expected a non-empty value at JSON path \" " + expression + "\" but found: []" );
208
175
}
209
176
210
177
@ Test
211
178
void assertValueIsNotEmptyForAnEmptyMap () {
212
179
String expression = "$.emptyMap" ;
213
180
assertThatExceptionOfType (AssertionError .class ).isThrownBy (() ->
214
- new JsonPathExpectationsHelper (expression ).assertValueIsNotEmpty (CONTENT ))
215
- .withMessageContaining ("Expected a non-empty value at JSON path \" " + expression + "\" but found: {}" );
181
+ new JsonPathExpectationsHelper (expression ).assertValueIsNotEmpty (CONTENT ))
182
+ .withMessageContaining ("Expected a non-empty value at JSON path \" " + expression + "\" but found: {}" );
216
183
}
217
184
218
185
@ Test
@@ -234,8 +201,8 @@ void hasJsonPathForIndefinitePathWithResults() {
234
201
void hasJsonPathForIndefinitePathWithEmptyResults () {
235
202
String expression = "$.familyMembers[?(@.name == 'Dilbert')]" ;
236
203
assertThatExceptionOfType (AssertionError .class ).isThrownBy (() ->
237
- new JsonPathExpectationsHelper (expression ).hasJsonPath (SIMPSONS ))
238
- .withMessageContaining ("No values for JSON path \" " + expression + "\" " );
204
+ new JsonPathExpectationsHelper (expression ).hasJsonPath (SIMPSONS ))
205
+ .withMessageContaining ("No values for JSON path \" " + expression + "\" " );
239
206
}
240
207
241
208
@ Test // SPR-16339
@@ -258,8 +225,8 @@ void doesNotHaveJsonPathForIndefinitePathWithEmptyResults() {
258
225
void doesNotHaveEmptyPathForIndefinitePathWithResults () {
259
226
String expression = "$.familyMembers[?(@.name == 'Bart')]" ;
260
227
assertThatExceptionOfType (AssertionError .class ).isThrownBy (() ->
261
- new JsonPathExpectationsHelper (expression ).doesNotHaveJsonPath (SIMPSONS ))
262
- .withMessageContaining ("Expected no values at JSON path \" " + expression + "\" " + "but found: [{\" name\" :\" Bart\" }]" );
228
+ new JsonPathExpectationsHelper (expression ).doesNotHaveJsonPath (SIMPSONS ))
229
+ .withMessageContaining ("Expected no values at JSON path \" " + expression + "\" " + "but found: [{\" name\" :\" Bart\" }]" );
263
230
}
264
231
265
232
@ Test
@@ -291,8 +258,8 @@ void assertValueIsStringForAnEmptyString() {
291
258
void assertValueIsStringForNonString () {
292
259
String expression = "$.bool" ;
293
260
assertThatExceptionOfType (AssertionError .class ).isThrownBy (() ->
294
- new JsonPathExpectationsHelper (expression ).assertValueIsString (CONTENT ))
295
- .withMessageContaining ("Expected a string at JSON path \" " + expression + "\" but found: true" );
261
+ new JsonPathExpectationsHelper (expression ).assertValueIsString (CONTENT ))
262
+ .withMessageContaining ("Expected a string at JSON path \" " + expression + "\" but found: true" );
296
263
}
297
264
298
265
@ Test
@@ -304,8 +271,8 @@ void assertValueIsNumber() {
304
271
void assertValueIsNumberForNonNumber () {
305
272
String expression = "$.bool" ;
306
273
assertThatExceptionOfType (AssertionError .class ).isThrownBy (() ->
307
- new JsonPathExpectationsHelper (expression ).assertValueIsNumber (CONTENT ))
308
- .withMessageContaining ("Expected a number at JSON path \" " + expression + "\" but found: true" );
274
+ new JsonPathExpectationsHelper (expression ).assertValueIsNumber (CONTENT ))
275
+ .withMessageContaining ("Expected a number at JSON path \" " + expression + "\" but found: true" );
309
276
}
310
277
311
278
@ Test
@@ -317,8 +284,8 @@ void assertValueIsBoolean() {
317
284
void assertValueIsBooleanForNonBoolean () {
318
285
String expression = "$.num" ;
319
286
assertThatExceptionOfType (AssertionError .class ).isThrownBy (() ->
320
- new JsonPathExpectationsHelper (expression ).assertValueIsBoolean (CONTENT ))
321
- .withMessageContaining ("Expected a boolean at JSON path \" " + expression + "\" but found: 5" );
287
+ new JsonPathExpectationsHelper (expression ).assertValueIsBoolean (CONTENT ))
288
+ .withMessageContaining ("Expected a boolean at JSON path \" " + expression + "\" but found: 5" );
322
289
}
323
290
324
291
@ Test
@@ -335,8 +302,8 @@ void assertValueIsArrayForAnEmptyArray() {
335
302
void assertValueIsArrayForNonArray () {
336
303
String expression = "$.str" ;
337
304
assertThatExceptionOfType (AssertionError .class ).isThrownBy (() ->
338
- new JsonPathExpectationsHelper (expression ).assertValueIsArray (CONTENT ))
339
- .withMessageContaining ("Expected an array at JSON path \" " + expression + "\" but found: 'foo'" );
305
+ new JsonPathExpectationsHelper (expression ).assertValueIsArray (CONTENT ))
306
+ .withMessageContaining ("Expected an array at JSON path \" " + expression + "\" but found: 'foo'" );
340
307
}
341
308
342
309
@ Test
@@ -353,8 +320,8 @@ void assertValueIsMapForAnEmptyMap() {
353
320
void assertValueIsMapForNonMap () {
354
321
String expression = "$.str" ;
355
322
assertThatExceptionOfType (AssertionError .class ).isThrownBy (() ->
356
- new JsonPathExpectationsHelper (expression ).assertValueIsMap (CONTENT ))
357
- .withMessageContaining ("Expected a map at JSON path \" " + expression + "\" but found: 'foo'" );
323
+ new JsonPathExpectationsHelper (expression ).assertValueIsMap (CONTENT ))
324
+ .withMessageContaining ("Expected a map at JSON path \" " + expression + "\" but found: 'foo'" );
358
325
}
359
326
360
327
}
0 commit comments