From 5a12e7b2c513731e9bc36589583c7e5b5d96059f Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sat, 1 Aug 2020 16:30:12 +0200 Subject: [PATCH 1/2] Fix typo in test method names --- .../util/JsonPathExpectationsHelperTests.java | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/spring-test/src/test/java/org/springframework/test/util/JsonPathExpectationsHelperTests.java b/spring-test/src/test/java/org/springframework/test/util/JsonPathExpectationsHelperTests.java index e6e9b2ba1fb..858a3834adc 100644 --- a/spring-test/src/test/java/org/springframework/test/util/JsonPathExpectationsHelperTests.java +++ b/spring-test/src/test/java/org/springframework/test/util/JsonPathExpectationsHelperTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2004-2019 the original author or authors. + * Copyright 2004-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -67,12 +67,12 @@ class JsonPathExpectationsHelperTests { } @Test - void existsForIndefinatePathWithResults() throws Exception { + void existsForIndefinitePathWithResults() throws Exception { new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Bart')]").exists(SIMPSONS); } @Test - void existsForIndefinatePathWithEmptyResults() throws Exception { + void existsForIndefinitePathWithEmptyResults() throws Exception { String expression = "$.familyMembers[?(@.name == 'Dilbert')]"; assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> new JsonPathExpectationsHelper(expression).exists(SIMPSONS)) @@ -101,7 +101,7 @@ class JsonPathExpectationsHelperTests { } @Test - void doesNotExistForIndefinatePathWithResults() throws Exception { + void doesNotExistForIndefinitePathWithResults() throws Exception { String expression = "$.familyMembers[?(@.name == 'Bart')]"; assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> new JsonPathExpectationsHelper(expression).doesNotExist(SIMPSONS)) @@ -109,7 +109,7 @@ class JsonPathExpectationsHelperTests { } @Test - void doesNotExistForIndefinatePathWithEmptyResults() throws Exception { + void doesNotExistForIndefinitePathWithEmptyResults() throws Exception { new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Dilbert')]").doesNotExist(SIMPSONS); } @@ -129,12 +129,12 @@ class JsonPathExpectationsHelperTests { } @Test - void assertValueIsEmptyForIndefinatePathWithEmptyResults() throws Exception { + void assertValueIsEmptyForIndefinitePathWithEmptyResults() throws Exception { new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Dilbert')]").assertValueIsEmpty(SIMPSONS); } @Test - void assertValueIsEmptyForIndefinatePathWithResults() throws Exception { + void assertValueIsEmptyForIndefinitePathWithResults() throws Exception { String expression = "$.familyMembers[?(@.name == 'Bart')]"; assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> new JsonPathExpectationsHelper(expression).assertValueIsEmpty(SIMPSONS)) @@ -175,12 +175,12 @@ class JsonPathExpectationsHelperTests { } @Test - void assertValueIsNotEmptyForIndefinatePathWithResults() throws Exception { + void assertValueIsNotEmptyForIndefinitePathWithResults() throws Exception { new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Bart')]").assertValueIsNotEmpty(SIMPSONS); } @Test - void assertValueIsNotEmptyForIndefinatePathWithEmptyResults() throws Exception { + void assertValueIsNotEmptyForIndefinitePathWithEmptyResults() throws Exception { String expression = "$.familyMembers[?(@.name == 'Dilbert')]"; assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> new JsonPathExpectationsHelper(expression).assertValueIsNotEmpty(SIMPSONS)) @@ -222,12 +222,12 @@ class JsonPathExpectationsHelperTests { } @Test - void hasJsonPathForIndefinatePathWithResults() { + void hasJsonPathForIndefinitePathWithResults() { new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Bart')]").hasJsonPath(SIMPSONS); } @Test - void hasJsonPathForIndefinatePathWithEmptyResults() { + void hasJsonPathForIndefinitePathWithEmptyResults() { String expression = "$.familyMembers[?(@.name == 'Dilbert')]"; assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> new JsonPathExpectationsHelper(expression).hasJsonPath(SIMPSONS)) @@ -246,12 +246,12 @@ class JsonPathExpectationsHelperTests { } @Test - void doesNotHaveJsonPathForIndefinatePathWithEmptyResults() { + void doesNotHaveJsonPathForIndefinitePathWithEmptyResults() { new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Dilbert')]").doesNotHaveJsonPath(SIMPSONS); } @Test - void doesNotHaveEmptyPathForIndefinatePathWithResults() { + void doesNotHaveEmptyPathForIndefinitePathWithResults() { String expression = "$.familyMembers[?(@.name == 'Bart')]"; assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> new JsonPathExpectationsHelper(expression).doesNotHaveJsonPath(SIMPSONS)) From 482adb94786b2166091675daf8e42e61e50ca8be Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sat, 1 Aug 2020 16:35:54 +0200 Subject: [PATCH 2/2] 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 of type cannot be converted to type Closes gh-25480 --- .../test/util/JsonPathExpectationsHelper.java | 15 ++++++++++++--- .../result/JsonPathResultMatchersTests.java | 14 +++++++++++--- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java b/spring-test/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java index 7a9ee1faaf9..af43cb787e9 100644 --- a/spring-test/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java +++ b/spring-test/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java @@ -26,6 +26,7 @@ import org.hamcrest.MatcherAssert; 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; @@ -108,10 +109,18 @@ public class JsonPathExpectationsHelper { } 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); } @@ -298,7 +307,7 @@ public class JsonPathExpectationsHelper { /** * Variant of {@link #evaluateJsonPath(String)} with a target type. - * This can be useful for matching numbers reliably for example coercing an + *

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 diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/result/JsonPathResultMatchersTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/result/JsonPathResultMatchersTests.java index 787d0a7632d..3fd1bfbcceb 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/result/JsonPathResultMatchersTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/result/JsonPathResultMatchersTests.java @@ -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: but was:"); + } + + @Test + public void valueWithTypeMismatch() throws Exception { + assertThatExceptionOfType(AssertionError.class) + .isThrownBy(() -> new JsonPathResultMatchers("$.str").value("bogus".getBytes()).match(stubMvcResult)) + .withMessage("At JSON path \"$.str\", value of type cannot be converted to type "); } @Test