Skip to content

Commit 91122ec

Browse files
committed
Add Consumer methods to WebTestClient assertion classes
Issue: SPR-16574
1 parent 20de500 commit 91122ec

File tree

9 files changed

+251
-96
lines changed

9 files changed

+251
-96
lines changed

spring-test/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,15 @@
2020
import java.util.Map;
2121

2222
import com.jayway.jsonpath.JsonPath;
23+
import org.hamcrest.CoreMatchers;
2324
import org.hamcrest.Matcher;
25+
import org.hamcrest.MatcherAssert;
2426

2527
import org.springframework.lang.Nullable;
2628
import org.springframework.util.Assert;
2729
import org.springframework.util.ObjectUtils;
2830
import org.springframework.util.StringUtils;
2931

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-
3632
/**
3733
* A helper class for applying assertions via JSON path expressions.
3834
*
@@ -74,7 +70,7 @@ public JsonPathExpectationsHelper(String expression, Object... args) {
7470
@SuppressWarnings("unchecked")
7571
public <T> void assertValue(String content, Matcher<T> matcher) {
7672
T value = (T) evaluateJsonPath(content);
77-
assertThat("JSON path \"" + this.expression + "\"", value, matcher);
73+
MatcherAssert.assertThat("JSON path \"" + this.expression + "\"", value, matcher);
7874
}
7975

8076
/**
@@ -89,7 +85,7 @@ public <T> void assertValue(String content, Matcher<T> matcher) {
8985
@SuppressWarnings("unchecked")
9086
public <T> void assertValue(String content, Matcher<T> matcher, Class<T> targetType) {
9187
T value = (T) evaluateJsonPath(content, targetType);
92-
assertThat("JSON path \"" + this.expression + "\"", value, matcher);
88+
MatcherAssert.assertThat("JSON path \"" + this.expression + "\"", value, matcher);
9389
}
9490

9591
/**
@@ -104,10 +100,11 @@ public void assertValue(String content, @Nullable Object expectedValue) {
104100
@SuppressWarnings("rawtypes")
105101
List actualValueList = (List) actualValue;
106102
if (actualValueList.isEmpty()) {
107-
fail("No matching value at JSON path \"" + this.expression + "\"");
103+
AssertionErrors.fail("No matching value at JSON path \"" + this.expression + "\"");
108104
}
109105
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);
111108
}
112109
actualValue = actualValueList.get(0);
113110
}
@@ -116,7 +113,7 @@ else if (actualValue != null && expectedValue != null) {
116113
actualValue = evaluateJsonPath(content, expectedValue.getClass());
117114
}
118115
}
119-
assertEquals("JSON path \"" + this.expression + "\"", expectedValue, actualValue);
116+
AssertionErrors.assertEquals("JSON path \"" + this.expression + "\"", expectedValue, actualValue);
120117
}
121118

122119
/**
@@ -127,7 +124,7 @@ else if (actualValue != null && expectedValue != null) {
127124
*/
128125
public void assertValueIsString(String content) {
129126
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));
131128
}
132129

133130
/**
@@ -138,7 +135,7 @@ public void assertValueIsString(String content) {
138135
*/
139136
public void assertValueIsBoolean(String content) {
140137
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));
142139
}
143140

144141
/**
@@ -149,7 +146,7 @@ public void assertValueIsBoolean(String content) {
149146
*/
150147
public void assertValueIsNumber(String content) {
151148
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));
153150
}
154151

155152
/**
@@ -159,7 +156,7 @@ public void assertValueIsNumber(String content) {
159156
*/
160157
public void assertValueIsArray(String content) {
161158
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));
163160
}
164161

165162
/**
@@ -170,7 +167,7 @@ public void assertValueIsArray(String content) {
170167
*/
171168
public void assertValueIsMap(String content) {
172169
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));
174171
}
175172

176173
/**
@@ -204,10 +201,10 @@ public void doesNotExist(String content) {
204201
}
205202
String reason = failureReason("no value", value);
206203
if (pathIsIndefinite() && value instanceof List) {
207-
assertTrue(reason, ((List<?>) value).isEmpty());
204+
AssertionErrors.assertTrue(reason, ((List<?>) value).isEmpty());
208205
}
209206
else {
210-
assertTrue(reason, (value == null));
207+
AssertionErrors.assertTrue(reason, (value == null));
211208
}
212209
}
213210

@@ -220,7 +217,7 @@ public void doesNotExist(String content) {
220217
*/
221218
public void assertValueIsEmpty(String content) {
222219
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));
224221
}
225222

226223
/**
@@ -232,7 +229,7 @@ public void assertValueIsEmpty(String content) {
232229
*/
233230
public void assertValueIsNotEmpty(String content) {
234231
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));
236233
}
237234

238235
/**
@@ -247,7 +244,8 @@ public void assertValueIsNotEmpty(String content) {
247244
public void hasJsonPath(String content) {
248245
Object value = evaluateJsonPath(content);
249246
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());
251249
}
252250
}
253251

@@ -270,10 +268,10 @@ public void doesNotHaveJsonPath(String content) {
270268
return;
271269
}
272270
if (pathIsIndefinite() && value instanceof List) {
273-
assertTrue(failureReason("no values", value), ((List<?>) value).isEmpty());
271+
AssertionErrors.assertTrue(failureReason("no values", value), ((List<?>) value).isEmpty());
274272
}
275273
else {
276-
fail(failureReason("no value", value));
274+
AssertionErrors.fail(failureReason("no value", value));
277275
}
278276
}
279277

@@ -282,18 +280,31 @@ private String failureReason(String expectedDescription, @Nullable Object value)
282280
ObjectUtils.nullSafeToString(StringUtils.quoteIfString(value)));
283281
}
284282

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+
*/
285289
@Nullable
286-
private Object evaluateJsonPath(String content) {
290+
public Object evaluateJsonPath(String content) {
287291
try {
288292
return this.jsonPath.read(content);
289293
}
290294
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);
293296
}
294297
}
295298

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) {
297308
try {
298309
return JsonPath.parse(content).read(this.expression, targetType);
299310
}
@@ -307,9 +318,9 @@ private Object evaluateJsonPath(String content, Class<?> targetType) {
307318
private Object assertExistsAndReturn(String content) {
308319
Object value = evaluateJsonPath(content);
309320
String reason = "No value at JSON path \"" + this.expression + "\"";
310-
assertTrue(reason, value != null);
321+
AssertionErrors.assertTrue(reason, value != null);
311322
if (pathIsIndefinite() && value instanceof List) {
312-
assertTrue(reason, !((List<?>) value).isEmpty());
323+
AssertionErrors.assertTrue(reason, !((List<?>) value).isEmpty());
313324
}
314325
return value;
315326
}

0 commit comments

Comments
 (0)