|
26 | 26 |
|
27 | 27 | import org.springframework.lang.Nullable;
|
28 | 28 | import org.springframework.util.Assert;
|
| 29 | +import org.springframework.util.ClassUtils; |
29 | 30 | import org.springframework.util.ObjectUtils;
|
30 | 31 | import org.springframework.util.StringUtils;
|
31 | 32 |
|
@@ -108,10 +109,18 @@ public void assertValue(String content, @Nullable Object expectedValue) {
|
108 | 109 | }
|
109 | 110 | actualValue = actualValueList.get(0);
|
110 | 111 | }
|
111 |
| - else if (actualValue != null && expectedValue != null) { |
112 |
| - if (!actualValue.getClass().equals(expectedValue.getClass())) { |
| 112 | + else if (actualValue != null && expectedValue != null && |
| 113 | + !actualValue.getClass().equals(expectedValue.getClass())) { |
| 114 | + try { |
113 | 115 | actualValue = evaluateJsonPath(content, expectedValue.getClass());
|
114 | 116 | }
|
| 117 | + catch (AssertionError error) { |
| 118 | + String message = String.format( |
| 119 | + "At JSON path \"%s\", value <%s> of type <%s> cannot be converted to type <%s>", |
| 120 | + this.expression, actualValue, ClassUtils.getDescriptiveType(actualValue), |
| 121 | + ClassUtils.getDescriptiveType(expectedValue)); |
| 122 | + throw new AssertionError(message, error.getCause()); |
| 123 | + } |
115 | 124 | }
|
116 | 125 | AssertionErrors.assertEquals("JSON path \"" + this.expression + "\"", expectedValue, actualValue);
|
117 | 126 | }
|
@@ -298,7 +307,7 @@ public Object evaluateJsonPath(String content) {
|
298 | 307 |
|
299 | 308 | /**
|
300 | 309 | * Variant of {@link #evaluateJsonPath(String)} with a target type.
|
301 |
| - * This can be useful for matching numbers reliably for example coercing an |
| 310 | + * <p>This can be useful for matching numbers reliably for example coercing an |
302 | 311 | * integer into a double.
|
303 | 312 | * @param content the content to evaluate against
|
304 | 313 | * @return the result of the evaluation
|
|
0 commit comments