Skip to content

Commit 482adb9

Browse files
committed
Fix error message for type mismatch in jsonPath().value()
Prior to this commit, if a value existed at the specified JSON path but had an incompatible type, the AssertionError thrown contained a message stating that the value did not exist (i.e., "No Value at JSON Path"), which was not only misleading but also technically incorrect. This commit fixes the error message for such use cases. For example, the AssertionError thrown in such use cases now resembles the following. At JSON path "$.name", value <Lisa> of type <java.lang.String> cannot be converted to type <byte[]> Closes gh-25480
1 parent 5a12e7b commit 482adb9

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import org.springframework.lang.Nullable;
2828
import org.springframework.util.Assert;
29+
import org.springframework.util.ClassUtils;
2930
import org.springframework.util.ObjectUtils;
3031
import org.springframework.util.StringUtils;
3132

@@ -108,10 +109,18 @@ public void assertValue(String content, @Nullable Object expectedValue) {
108109
}
109110
actualValue = actualValueList.get(0);
110111
}
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 {
113115
actualValue = evaluateJsonPath(content, expectedValue.getClass());
114116
}
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+
}
115124
}
116125
AssertionErrors.assertEquals("JSON path \"" + this.expression + "\"", expectedValue, actualValue);
117126
}
@@ -298,7 +307,7 @@ public Object evaluateJsonPath(String content) {
298307

299308
/**
300309
* 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
302311
* integer into a double.
303312
* @param content the content to evaluate against
304313
* @return the result of the evaluation

spring-test/src/test/java/org/springframework/test/web/servlet/result/JsonPathResultMatchersTests.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,17 @@ public class JsonPathResultMatchersTests {
6464
}
6565

6666
@Test
67-
public void valueWithMismatch() throws Exception {
68-
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
69-
new JsonPathResultMatchers("$.str").value("bogus").match(stubMvcResult));
67+
public void valueWithValueMismatch() throws Exception {
68+
assertThatExceptionOfType(AssertionError.class)
69+
.isThrownBy(() -> new JsonPathResultMatchers("$.str").value("bogus").match(stubMvcResult))
70+
.withMessage("JSON path \"$.str\" expected:<bogus> but was:<foo>");
71+
}
72+
73+
@Test
74+
public void valueWithTypeMismatch() throws Exception {
75+
assertThatExceptionOfType(AssertionError.class)
76+
.isThrownBy(() -> new JsonPathResultMatchers("$.str").value("bogus".getBytes()).match(stubMvcResult))
77+
.withMessage("At JSON path \"$.str\", value <foo> of type <java.lang.String> cannot be converted to type <byte[]>");
7078
}
7179

7280
@Test

0 commit comments

Comments
 (0)