Skip to content

Commit

Permalink
Fix error message for type mismatch in jsonPath().value()
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sbrannen committed Aug 1, 2020
1 parent 5a12e7b commit 482adb9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
Expand Up @@ -26,6 +26,7 @@

import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

Expand Down Expand Up @@ -108,10 +109,18 @@ public void assertValue(String content, @Nullable Object expectedValue) {
}
actualValue = actualValueList.get(0);
}
else if (actualValue != null && expectedValue != null) {
if (!actualValue.getClass().equals(expectedValue.getClass())) {
else if (actualValue != null && expectedValue != null &&
!actualValue.getClass().equals(expectedValue.getClass())) {
try {
actualValue = evaluateJsonPath(content, expectedValue.getClass());
}
catch (AssertionError error) {
String message = String.format(
"At JSON path \"%s\", value <%s> of type <%s> cannot be converted to type <%s>",
this.expression, actualValue, ClassUtils.getDescriptiveType(actualValue),
ClassUtils.getDescriptiveType(expectedValue));
throw new AssertionError(message, error.getCause());
}
}
AssertionErrors.assertEquals("JSON path \"" + this.expression + "\"", expectedValue, actualValue);
}
Expand Down Expand Up @@ -298,7 +307,7 @@ public Object evaluateJsonPath(String content) {

/**
* Variant of {@link #evaluateJsonPath(String)} with a target type.
* This can be useful for matching numbers reliably for example coercing an
* <p>This can be useful for matching numbers reliably for example coercing an
* integer into a double.
* @param content the content to evaluate against
* @return the result of the evaluation
Expand Down
Expand Up @@ -64,9 +64,17 @@ public class JsonPathResultMatchersTests {
}

@Test
public void valueWithMismatch() throws Exception {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new JsonPathResultMatchers("$.str").value("bogus").match(stubMvcResult));
public void valueWithValueMismatch() throws Exception {
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> new JsonPathResultMatchers("$.str").value("bogus").match(stubMvcResult))
.withMessage("JSON path \"$.str\" expected:<bogus> but was:<foo>");
}

@Test
public void valueWithTypeMismatch() throws Exception {
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> new JsonPathResultMatchers("$.str").value("bogus".getBytes()).match(stubMvcResult))
.withMessage("At JSON path \"$.str\", value <foo> of type <java.lang.String> cannot be converted to type <byte[]>");
}

@Test
Expand Down

0 comments on commit 482adb9

Please sign in to comment.