Skip to content

Commit faba044

Browse files
committed
Polish JsonPathExpectationsHelperTests
1 parent 4d885f9 commit faba044

File tree

1 file changed

+52
-85
lines changed

1 file changed

+52
-85
lines changed

spring-test/src/test/java/org/springframework/test/util/JsonPathExpectationsHelperTests.java

Lines changed: 52 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.springframework.test.util;
1818

1919
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.params.ParameterizedTest;
21+
import org.junit.jupiter.params.provider.ValueSource;
2022

2123
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2224
import static org.hamcrest.core.Is.is;
@@ -55,19 +57,10 @@ class JsonPathExpectationsHelperTests {
5557
}""";
5658

5759

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);
7164
}
7265

7366
@Test
@@ -79,57 +72,49 @@ void existsForIndefinitePathWithResults() {
7972
void existsForIndefinitePathWithEmptyResults() {
8073
String expression = "$.familyMembers[?(@.name == 'Dilbert')]";
8174
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 + "\"");
8477
}
8578

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);
8983
}
9084

9185
@Test
9286
void doesNotExistForAnEmptyArray() {
9387
String expression = "$.emptyArray";
9488
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: []");
9791
}
9892

9993
@Test
10094
void doesNotExistForAnEmptyMap() {
10195
String expression = "$.emptyMap";
10296
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: {}");
10599
}
106100

107101
@Test
108102
void doesNotExistForIndefinitePathWithResults() {
109103
String expression = "$.familyMembers[?(@.name == 'Bart')]";
110104
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\"}]");
113107
}
114108

115109
@Test
116110
void doesNotExistForIndefinitePathWithEmptyResults() {
117111
new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Dilbert')]").doesNotExist(SIMPSONS);
118112
}
119113

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);
133118
}
134119

135120
@Test
@@ -141,41 +126,23 @@ void assertValueIsEmptyForIndefinitePathWithEmptyResults() {
141126
void assertValueIsEmptyForIndefinitePathWithResults() {
142127
String expression = "$.familyMembers[?(@.name == 'Bart')]";
143128
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\"}]");
146131
}
147132

148133
@Test
149134
void assertValueIsEmptyForWhitespace() {
150135
String expression = "$.whitespace";
151136
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: ' '");
154139
}
155140

156-
@Test
157-
void assertValueIsNotEmptyForString() {
158-
new JsonPathExpectationsHelper("$.str").assertValueIsNotEmpty(CONTENT);
159-
}
160141

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);
179146
}
180147

181148
@Test
@@ -187,32 +154,32 @@ void assertValueIsNotEmptyForIndefinitePathWithResults() {
187154
void assertValueIsNotEmptyForIndefinitePathWithEmptyResults() {
188155
String expression = "$.familyMembers[?(@.name == 'Dilbert')]";
189156
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: []");
192159
}
193160

194161
@Test
195162
void assertValueIsNotEmptyForAnEmptyString() {
196163
String expression = "$.emptyString";
197164
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: ''");
200167
}
201168

202169
@Test
203170
void assertValueIsNotEmptyForAnEmptyArray() {
204171
String expression = "$.emptyArray";
205172
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: []");
208175
}
209176

210177
@Test
211178
void assertValueIsNotEmptyForAnEmptyMap() {
212179
String expression = "$.emptyMap";
213180
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: {}");
216183
}
217184

218185
@Test
@@ -234,8 +201,8 @@ void hasJsonPathForIndefinitePathWithResults() {
234201
void hasJsonPathForIndefinitePathWithEmptyResults() {
235202
String expression = "$.familyMembers[?(@.name == 'Dilbert')]";
236203
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 + "\"");
239206
}
240207

241208
@Test // SPR-16339
@@ -258,8 +225,8 @@ void doesNotHaveJsonPathForIndefinitePathWithEmptyResults() {
258225
void doesNotHaveEmptyPathForIndefinitePathWithResults() {
259226
String expression = "$.familyMembers[?(@.name == 'Bart')]";
260227
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\"}]");
263230
}
264231

265232
@Test
@@ -291,8 +258,8 @@ void assertValueIsStringForAnEmptyString() {
291258
void assertValueIsStringForNonString() {
292259
String expression = "$.bool";
293260
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");
296263
}
297264

298265
@Test
@@ -304,8 +271,8 @@ void assertValueIsNumber() {
304271
void assertValueIsNumberForNonNumber() {
305272
String expression = "$.bool";
306273
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");
309276
}
310277

311278
@Test
@@ -317,8 +284,8 @@ void assertValueIsBoolean() {
317284
void assertValueIsBooleanForNonBoolean() {
318285
String expression = "$.num";
319286
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");
322289
}
323290

324291
@Test
@@ -335,8 +302,8 @@ void assertValueIsArrayForAnEmptyArray() {
335302
void assertValueIsArrayForNonArray() {
336303
String expression = "$.str";
337304
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'");
340307
}
341308

342309
@Test
@@ -353,8 +320,8 @@ void assertValueIsMapForAnEmptyMap() {
353320
void assertValueIsMapForNonMap() {
354321
String expression = "$.str";
355322
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'");
358325
}
359326

360327
}

0 commit comments

Comments
 (0)