Skip to content

Commit d8554c4

Browse files
committed
Merge branch '5.2.x'
2 parents 58412af + 482adb9 commit d8554c4

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
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/util/JsonPathExpectationsHelperTests.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2004-2019 the original author or authors.
2+
* Copyright 2004-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -67,12 +67,12 @@ void existsForAnEmptyMap() throws Exception {
6767
}
6868

6969
@Test
70-
void existsForIndefinatePathWithResults() throws Exception {
70+
void existsForIndefinitePathWithResults() throws Exception {
7171
new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Bart')]").exists(SIMPSONS);
7272
}
7373

7474
@Test
75-
void existsForIndefinatePathWithEmptyResults() throws Exception {
75+
void existsForIndefinitePathWithEmptyResults() throws Exception {
7676
String expression = "$.familyMembers[?(@.name == 'Dilbert')]";
7777
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
7878
new JsonPathExpectationsHelper(expression).exists(SIMPSONS))
@@ -101,15 +101,15 @@ void doesNotExistForAnEmptyMap() throws Exception {
101101
}
102102

103103
@Test
104-
void doesNotExistForIndefinatePathWithResults() throws Exception {
104+
void doesNotExistForIndefinitePathWithResults() throws Exception {
105105
String expression = "$.familyMembers[?(@.name == 'Bart')]";
106106
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
107107
new JsonPathExpectationsHelper(expression).doesNotExist(SIMPSONS))
108108
.withMessageContaining("Expected no value at JSON path \"" + expression + "\" but found: [{\"name\":\"Bart\"}]");
109109
}
110110

111111
@Test
112-
void doesNotExistForIndefinatePathWithEmptyResults() throws Exception {
112+
void doesNotExistForIndefinitePathWithEmptyResults() throws Exception {
113113
new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Dilbert')]").doesNotExist(SIMPSONS);
114114
}
115115

@@ -129,12 +129,12 @@ void assertValueIsEmptyForAnEmptyMap() throws Exception {
129129
}
130130

131131
@Test
132-
void assertValueIsEmptyForIndefinatePathWithEmptyResults() throws Exception {
132+
void assertValueIsEmptyForIndefinitePathWithEmptyResults() throws Exception {
133133
new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Dilbert')]").assertValueIsEmpty(SIMPSONS);
134134
}
135135

136136
@Test
137-
void assertValueIsEmptyForIndefinatePathWithResults() throws Exception {
137+
void assertValueIsEmptyForIndefinitePathWithResults() throws Exception {
138138
String expression = "$.familyMembers[?(@.name == 'Bart')]";
139139
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
140140
new JsonPathExpectationsHelper(expression).assertValueIsEmpty(SIMPSONS))
@@ -175,12 +175,12 @@ void assertValueIsNotEmptyForMap() throws Exception {
175175
}
176176

177177
@Test
178-
void assertValueIsNotEmptyForIndefinatePathWithResults() throws Exception {
178+
void assertValueIsNotEmptyForIndefinitePathWithResults() throws Exception {
179179
new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Bart')]").assertValueIsNotEmpty(SIMPSONS);
180180
}
181181

182182
@Test
183-
void assertValueIsNotEmptyForIndefinatePathWithEmptyResults() throws Exception {
183+
void assertValueIsNotEmptyForIndefinitePathWithEmptyResults() throws Exception {
184184
String expression = "$.familyMembers[?(@.name == 'Dilbert')]";
185185
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
186186
new JsonPathExpectationsHelper(expression).assertValueIsNotEmpty(SIMPSONS))
@@ -222,12 +222,12 @@ void hasJsonPathWithNull() {
222222
}
223223

224224
@Test
225-
void hasJsonPathForIndefinatePathWithResults() {
225+
void hasJsonPathForIndefinitePathWithResults() {
226226
new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Bart')]").hasJsonPath(SIMPSONS);
227227
}
228228

229229
@Test
230-
void hasJsonPathForIndefinatePathWithEmptyResults() {
230+
void hasJsonPathForIndefinitePathWithEmptyResults() {
231231
String expression = "$.familyMembers[?(@.name == 'Dilbert')]";
232232
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
233233
new JsonPathExpectationsHelper(expression).hasJsonPath(SIMPSONS))
@@ -246,12 +246,12 @@ void doesNotHaveJsonPathWithNull() {
246246
}
247247

248248
@Test
249-
void doesNotHaveJsonPathForIndefinatePathWithEmptyResults() {
249+
void doesNotHaveJsonPathForIndefinitePathWithEmptyResults() {
250250
new JsonPathExpectationsHelper("$.familyMembers[?(@.name == 'Dilbert')]").doesNotHaveJsonPath(SIMPSONS);
251251
}
252252

253253
@Test
254-
void doesNotHaveEmptyPathForIndefinatePathWithResults() {
254+
void doesNotHaveEmptyPathForIndefinitePathWithResults() {
255255
String expression = "$.familyMembers[?(@.name == 'Bart')]";
256256
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
257257
new JsonPathExpectationsHelper(expression).doesNotHaveJsonPath(SIMPSONS))

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)