Skip to content

Commit 276e99d

Browse files
committed
Additional Testing Methods Added To JsonPathResultMatchers
JsonPathResultMatchers has some useful methods: * doesNotExist * exists * isArray This commit adds a few more useful methods: * isBoolean * isNumber * isMap * isString Issue: SPR-13320
1 parent 1a659e3 commit 276e99d

File tree

3 files changed

+139
-3
lines changed

3 files changed

+139
-3
lines changed

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616

1717
package org.springframework.test.util;
1818

19+
import java.lang.Boolean;
20+
import java.lang.Number;
21+
import java.lang.String;
1922
import java.lang.reflect.Array;
2023
import java.lang.reflect.Method;
2124
import java.text.ParseException;
2225
import java.util.List;
26+
import java.util.Map;
2327

2428
import com.jayway.jsonpath.InvalidPathException;
2529
import com.jayway.jsonpath.JsonPath;
@@ -28,6 +32,7 @@
2832
import org.springframework.util.Assert;
2933
import org.springframework.util.ReflectionUtils;
3034

35+
import static org.hamcrest.core.IsInstanceOf.instanceOf;
3136
import static org.hamcrest.MatcherAssert.*;
3237
import static org.springframework.test.util.AssertionErrors.*;
3338

@@ -138,7 +143,47 @@ public void assertValueIsArray(String responseContent) throws ParseException {
138143
Object actualValue = evaluateJsonPath(responseContent);
139144
assertTrue("No value for JSON path \"" + this.expression + "\"", actualValue != null);
140145
String reason = "Expected array at JSON path " + this.expression + " but found " + actualValue;
141-
assertTrue(reason, actualValue instanceof List);
146+
assertThat(reason, actualValue, instanceOf(List.class));
147+
}
148+
149+
/**
150+
* Apply the JSON path and assert the resulting value is a boolean.
151+
*/
152+
public void assertValueIsBoolean(String responseContent) throws ParseException {
153+
Object actualValue = evaluateJsonPath(responseContent);
154+
assertTrue("No value for JSON path \"" + this.expression + "\"", actualValue != null);
155+
String reason = "Expected array at JSON path " + this.expression + " but found " + actualValue;
156+
assertThat(reason, actualValue, instanceOf(Boolean.class));
157+
}
158+
159+
/**
160+
* Apply the JSON path and assert the resulting value is a number.
161+
*/
162+
public void assertValueIsNumber(String responseContent) throws ParseException {
163+
Object actualValue = evaluateJsonPath(responseContent);
164+
assertTrue("No value for JSON path \"" + this.expression + "\"", actualValue != null);
165+
String reason = "Expected array at JSON path " + this.expression + " but found " + actualValue;
166+
assertThat(reason, actualValue, instanceOf(Number.class));
167+
}
168+
169+
/**
170+
* Apply the JSON path and assert the resulting value is a map.
171+
*/
172+
public void assertValueIsMap(String responseContent) throws ParseException {
173+
Object actualValue = evaluateJsonPath(responseContent);
174+
assertTrue("No value for JSON path \"" + this.expression + "\"", actualValue != null);
175+
String reason = "Expected array at JSON path " + this.expression + " but found " + actualValue;
176+
assertThat(reason, actualValue, instanceOf(Map.class));
177+
}
178+
179+
/**
180+
* Apply the JSON path and assert the resulting value is a string.
181+
*/
182+
public void assertValueIsString(String responseContent) throws ParseException {
183+
Object actualValue = evaluateJsonPath(responseContent);
184+
assertTrue("No value for JSON path \"" + this.expression + "\"", actualValue != null);
185+
String reason = "Expected array at JSON path " + this.expression + " but found " + actualValue;
186+
assertThat(reason, actualValue, instanceOf(String.class));
142187
}
143188

144189
/**

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

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public void match(MvcResult result) throws Exception {
9999
}
100100

101101
/**
102-
* Evluate the JSON path and assert the content found is an array.
102+
* Evaluate the JSON path and assert the content found is an array.
103103
*/
104104
public ResultMatcher isArray() {
105105
return new ResultMatcher() {
@@ -111,4 +111,56 @@ public void match(MvcResult result) throws Exception {
111111
};
112112
}
113113

114+
/**
115+
* Evaluate the JSON path and assert the content found is a boolean.
116+
*/
117+
public ResultMatcher isBoolean() {
118+
return new ResultMatcher() {
119+
@Override
120+
public void match(MvcResult result) throws Exception {
121+
String content = result.getResponse().getContentAsString();
122+
jsonPathHelper.assertValueIsBoolean(content);
123+
}
124+
};
125+
}
126+
127+
/**
128+
* Evaluate the JSON path and assert the content found is a number.
129+
*/
130+
public ResultMatcher isNumber() {
131+
return new ResultMatcher() {
132+
@Override
133+
public void match(MvcResult result) throws Exception {
134+
String content = result.getResponse().getContentAsString();
135+
jsonPathHelper.assertValueIsNumber(content);
136+
}
137+
};
138+
}
139+
140+
/**
141+
* Evaluate the JSON path and assert the content found is a map.
142+
*/
143+
public ResultMatcher isMap() {
144+
return new ResultMatcher() {
145+
@Override
146+
public void match(MvcResult result) throws Exception {
147+
String content = result.getResponse().getContentAsString();
148+
jsonPathHelper.assertValueIsMap(content);
149+
}
150+
};
151+
}
152+
153+
/**
154+
* Evaluate the JSON path and assert the content found is a string.
155+
*/
156+
public ResultMatcher isString() {
157+
return new ResultMatcher() {
158+
@Override
159+
public void match(MvcResult result) throws Exception {
160+
String content = result.getResponse().getContentAsString();
161+
jsonPathHelper.assertValueIsString(content);
162+
}
163+
};
164+
}
165+
114166
}

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,47 @@ public void isArrayNoMatch() throws Exception {
7979
new JsonPathResultMatchers("$.bar").isArray().match(getStubMvcResult());
8080
}
8181

82+
@Test
83+
public void isBooleanMatch() throws Exception {
84+
new JsonPathResultMatchers("$.icanhaz").isBoolean().match(getStubMvcResult());
85+
}
86+
87+
@Test(expected=AssertionError.class)
88+
public void isBooleanNoMatch() throws Exception {
89+
new JsonPathResultMatchers("$.foo").isBoolean().match(getStubMvcResult());
90+
}
91+
92+
@Test
93+
public void isNumberMatch() throws Exception {
94+
new JsonPathResultMatchers("$.howmanies").isNumber().match(getStubMvcResult());
95+
}
96+
97+
@Test(expected=AssertionError.class)
98+
public void isNumberNoMatch() throws Exception {
99+
new JsonPathResultMatchers("$.foo").isNumber().match(getStubMvcResult());
100+
}
101+
102+
@Test
103+
public void isMapMatch() throws Exception {
104+
new JsonPathResultMatchers("$.cheeseburger").isMap().match(getStubMvcResult());
105+
}
106+
107+
@Test(expected=AssertionError.class)
108+
public void isMapNoMatch() throws Exception {
109+
new JsonPathResultMatchers("$.foo").isMap().match(getStubMvcResult());
110+
}
111+
112+
@Test
113+
public void isStringMatch() throws Exception {
114+
new JsonPathResultMatchers("$.foo").isString().match(getStubMvcResult());
115+
}
116+
117+
@Test(expected=AssertionError.class)
118+
public void isStringNoMatch() throws Exception {
119+
new JsonPathResultMatchers("$.qux").isString().match(getStubMvcResult());
120+
}
82121

83-
private static final String RESPONSE_CONTENT = "{\"foo\":\"bar\", \"qux\":[\"baz1\",\"baz2\"]}";
122+
private static final String RESPONSE_CONTENT = "{\"foo\":\"bar\", \"qux\":[\"baz1\",\"baz2\"], \"icanhaz\":true, \"howmanies\": 5, \"cheeseburger\": {\"pickles\": true} }";
84123

85124
private StubMvcResult getStubMvcResult() throws Exception {
86125
MockHttpServletResponse response = new MockHttpServletResponse();

0 commit comments

Comments
 (0)