Skip to content

Commit b47d97c

Browse files
committed
Improve error message for JSON path expressions
Issue: SPR-SPR-10275
1 parent ccca82b commit b47d97c

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
package org.springframework.test.util;
1818

1919
import static org.springframework.test.util.AssertionErrors.assertEquals;
20-
import static org.springframework.test.util.AssertionErrors.*;
20+
import static org.springframework.test.util.AssertionErrors.assertTrue;
21+
import static org.springframework.test.util.AssertionErrors.fail;
2122
import static org.springframework.test.util.MatcherAssertionErrors.assertThat;
2223

2324
import java.text.ParseException;
@@ -96,6 +97,10 @@ public void assertValue(String responseContent, Object expectedValue) throws Par
9697
}
9798
actualValue = actualValueList.get(0);
9899
}
100+
else if (actualValue != null && expectedValue != null) {
101+
assertEquals("For JSON path " + this.expression + " type of value",
102+
expectedValue.getClass(), actualValue.getClass());
103+
}
99104
assertEquals("JSON path" + this.expression, expectedValue, actualValue);
100105
}
101106

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2004-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.util;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.fail;
21+
22+
import org.junit.Test;
23+
24+
25+
/**
26+
* Test fixture for {@link JsonPathExpectationsHelper}.
27+
*
28+
* @author Rossen Stoyanchev
29+
*/
30+
public class JsonPathExpectationsHelperTests {
31+
32+
33+
@Test
34+
public void test() throws Exception {
35+
try {
36+
new JsonPathExpectationsHelper("$.nr").assertValue("{ \"nr\" : 5 }", "5");
37+
fail("Expected exception");
38+
}
39+
catch (AssertionError ex) {
40+
assertEquals("For JSON path $.nr type of value expected:<class java.lang.String> but was:<class java.lang.Integer>",
41+
ex.getMessage());
42+
}
43+
}
44+
45+
}

0 commit comments

Comments
 (0)