Consider empty arrays as existent in JsonPath assertions
Prior to this commit, a JsonPath assertion that a path expression evaluated to an array in JsonPathExpectationsHelper (and therefore indirectly in JsonPathResultMatchers in Spring MVC Test) would incorrectly fail if the array was present in the JSON content but empty. This commit fixes this issue by removing the "not empty" check for arrays and lists. Issue: SPR-13320
This commit is contained in:
parent
8afea1bc78
commit
5a05cdbedb
|
@ -238,9 +238,6 @@ public class JsonPathExpectationsHelper {
|
||||||
Object value = evaluateJsonPath(content);
|
Object value = evaluateJsonPath(content);
|
||||||
String reason = "No value for JSON path \"" + this.expression + "\"";
|
String reason = "No value for JSON path \"" + this.expression + "\"";
|
||||||
assertTrue(reason, value != null);
|
assertTrue(reason, value != null);
|
||||||
if (List.class.isInstance(value)) {
|
|
||||||
assertTrue(reason, !((List<?>) value).isEmpty());
|
|
||||||
}
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -106,6 +106,11 @@ public class JsonPathExpectationsHelperTests {
|
||||||
new JsonPathExpectationsHelper("$.arr").assertValueIsArray(CONTENT);
|
new JsonPathExpectationsHelper("$.arr").assertValueIsArray(CONTENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void assertValueIsArrayForAnEmptyArray() throws Exception {
|
||||||
|
new JsonPathExpectationsHelper("$.emptyArray").assertValueIsArray(CONTENT);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void assertValueIsArrayForNonArray() throws Exception {
|
public void assertValueIsArrayForNonArray() throws Exception {
|
||||||
exception.expect(AssertionError.class);
|
exception.expect(AssertionError.class);
|
||||||
|
@ -117,6 +122,11 @@ public class JsonPathExpectationsHelperTests {
|
||||||
new JsonPathExpectationsHelper("$.colorMap").assertValueIsMap(CONTENT);
|
new JsonPathExpectationsHelper("$.colorMap").assertValueIsMap(CONTENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void assertValueIsMapForAnEmptyMap() throws Exception {
|
||||||
|
new JsonPathExpectationsHelper("$.emptyMap").assertValueIsMap(CONTENT);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void assertValueIsMapForNonMap() throws Exception {
|
public void assertValueIsMapForNonMap() throws Exception {
|
||||||
exception.expect(AssertionError.class);
|
exception.expect(AssertionError.class);
|
||||||
|
|
|
@ -32,7 +32,7 @@ import org.springframework.test.web.servlet.StubMvcResult;
|
||||||
*/
|
*/
|
||||||
public class JsonPathResultMatchersTests {
|
public class JsonPathResultMatchersTests {
|
||||||
|
|
||||||
private static final String RESPONSE_CONTENT = "{\"foo\": \"bar\", \"qux\": [\"baz\"], \"emptyArray\": [], \"icanhaz\": true, \"howmanies\": 5, \"cheeseburger\": {\"pickles\": true} }";
|
private static final String RESPONSE_CONTENT = "{\"foo\": \"bar\", \"qux\": [\"baz\"], \"emptyArray\": [], \"icanhaz\": true, \"howmanies\": 5, \"cheeseburger\": {\"pickles\": true}, \"emptyMap\": {} }";
|
||||||
|
|
||||||
private static final StubMvcResult stubMvcResult;
|
private static final StubMvcResult stubMvcResult;
|
||||||
|
|
||||||
|
@ -93,6 +93,10 @@ public class JsonPathResultMatchersTests {
|
||||||
public void isArray() throws Exception {
|
public void isArray() throws Exception {
|
||||||
new JsonPathResultMatchers("$.qux").isArray().match(stubMvcResult);
|
new JsonPathResultMatchers("$.qux").isArray().match(stubMvcResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isArrayForAnEmptyArray() throws Exception {
|
||||||
|
new JsonPathResultMatchers("$.emptyArray").isArray().match(stubMvcResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = AssertionError.class)
|
@Test(expected = AssertionError.class)
|
||||||
|
@ -100,6 +104,21 @@ public class JsonPathResultMatchersTests {
|
||||||
new JsonPathResultMatchers("$.bar").isArray().match(stubMvcResult);
|
new JsonPathResultMatchers("$.bar").isArray().match(stubMvcResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isMap() throws Exception {
|
||||||
|
new JsonPathResultMatchers("$.cheeseburger").isMap().match(stubMvcResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isMapForAnEmptyMap() throws Exception {
|
||||||
|
new JsonPathResultMatchers("$.emptyMap").isMap().match(stubMvcResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = AssertionError.class)
|
||||||
|
public void isMapNoMatch() throws Exception {
|
||||||
|
new JsonPathResultMatchers("$.foo").isMap().match(stubMvcResult);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void isBoolean() throws Exception {
|
public void isBoolean() throws Exception {
|
||||||
new JsonPathResultMatchers("$.icanhaz").isBoolean().match(stubMvcResult);
|
new JsonPathResultMatchers("$.icanhaz").isBoolean().match(stubMvcResult);
|
||||||
|
@ -120,16 +139,6 @@ public class JsonPathResultMatchersTests {
|
||||||
new JsonPathResultMatchers("$.foo").isNumber().match(stubMvcResult);
|
new JsonPathResultMatchers("$.foo").isNumber().match(stubMvcResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void isMap() throws Exception {
|
|
||||||
new JsonPathResultMatchers("$.cheeseburger").isMap().match(stubMvcResult);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(expected = AssertionError.class)
|
|
||||||
public void isMapNoMatch() throws Exception {
|
|
||||||
new JsonPathResultMatchers("$.foo").isMap().match(stubMvcResult);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void isString() throws Exception {
|
public void isString() throws Exception {
|
||||||
new JsonPathResultMatchers("$.foo").isString().match(stubMvcResult);
|
new JsonPathResultMatchers("$.foo").isString().match(stubMvcResult);
|
||||||
|
|
Loading…
Reference in New Issue