Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add isArray/isNotArray to JsonPathResultMatchers (fix #28) #29

Merged
merged 1 commit into from

2 participants

@tbruyelle

Assert that a json path is or is not an array.
Also add corresponding tests in JsonPathResultMatchersTests.

@rstoyanchev
Collaborator

Looks good although I think we can do without isNotArray(). Having isArray() is helpful because it's not obvious how to do it but since there is nothing more to the negation than not(isArray()), then let's keep it out.

@tbruyelle tbruyelle Fix #28 Add isArray to jsonPath
Assert that a json path is an array.
Also add corresponding tests.
ae7bc0d
@tbruyelle

No problem I removed isNotArray() and rebased the commits.

@rstoyanchev rstoyanchev merged commit 036fef5 into from
@rstoyanchev
Collaborator

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 21, 2012
  1. @tbruyelle

    Fix #28 Add isArray to jsonPath

    tbruyelle authored
    Assert that a json path is an array.
    Also add corresponding tests.
This page is out of date. Refresh to see the latest.
View
12 src/main/java/org/springframework/test/web/server/result/JsonPathResultMatchers.java
@@ -22,6 +22,10 @@
import org.springframework.test.web.server.ResultMatcher;
import org.springframework.test.web.support.JsonPathExpectationsHelper;
+import java.util.List;
+
+import static org.hamcrest.Matchers.*;
+
/**
* TODO ...
@@ -55,7 +59,7 @@ public void match(MvcResult result) throws Exception {
* TODO
*/
public ResultMatcher value(Object value) {
- return value(Matchers.equalTo(value));
+ return value(equalTo(value));
}
/**
@@ -82,4 +86,10 @@ public void match(MvcResult result) throws Exception {
};
}
+ /**
+ * Assert a json path is an array
+ */
+ public ResultMatcher isArray() {
+ return value(isA(List.class));
+ }
}
View
11 src/test/java/org/springframework/test/web/server/result/JsonPathResultMatchersTests.java
@@ -66,8 +66,17 @@ public void doesNotExistNoMatch() throws Exception {
new JsonPathResultMatchers("$.foo").doesNotExist().match(getStubMvcResult());
}
+ @Test
+ public void isArrayMatch() throws Exception {
+ new JsonPathResultMatchers("$.qux").isArray().match(getStubMvcResult());
+ }
+
+ @Test(expected=AssertionError.class)
+ public void isArrayNoMatch() throws Exception {
+ new JsonPathResultMatchers("$.bar").isArray().match(getStubMvcResult());
+ }
- private static final String CONTENT = "{\"foo\":\"bar\"}";
+ private static final String CONTENT = "{\"foo\":\"bar\", \"qux\":[\"baz1\",\"baz2\"]}";
private StubMvcResult getStubMvcResult() throws Exception {
MockHttpServletResponse response = new MockHttpServletResponse();
Something went wrong with that request. Please try again.