Browse Source

improved tests

pull/1/merge
kalle 14 years ago
parent
commit
c01457fad2
  1. 13
      json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java
  2. 5
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java

13
json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java

@ -1,5 +1,6 @@
package com.jayway.jsonassert; package com.jayway.jsonassert;
import org.hamcrest.Matchers;
import org.junit.Test; import org.junit.Test;
import static com.jayway.jsonassert.JsonAssert.*; import static com.jayway.jsonassert.JsonAssert.*;
@ -63,7 +64,7 @@ public class JsonAssertTest {
@Test @Test
public void list_content_can_be_asserted_with_nested_matcher() throws Exception { public void list_content_can_be_asserted_with_nested_matcher() throws Exception {
with(JSON).assertThat("$..book[*]", hasItems( hasEntry("author", "Nigel Rees"), hasEntry("author", "Evelyn Waugh")) ); with(JSON).assertThat("$..book[*]", hasItems(hasEntry("author", "Nigel Rees"), hasEntry("author", "Evelyn Waugh")));
} }
@Test @Test
@ -91,4 +92,14 @@ public class JsonAssertTest {
.assertThat("$.store.book[0].title", equalTo("Sayings of the Century")); .assertThat("$.store.book[0].title", equalTo("Sayings of the Century"));
} }
@Test
public void no_hit_returns_null() throws Exception {
with(JSON).assertThat("$.store.book[1000].title", Matchers.<Object>nullValue());
}
@Test
public void invalid_path() throws Exception {
with(JSON).assertThat("$.store.book[*].fooBar", emptyCollection());
}
} }

5
json-path/src/main/java/com/jayway/jsonpath/JsonPath.java

@ -172,9 +172,12 @@ public class JsonPath {
} }
} }
if (result.size() != 1) { if (result.size() > 1) {
throw new RuntimeException(format("Expected one result when reading path: %s but was: ", jsonPath, result.size())); throw new RuntimeException(format("Expected one result when reading path: %s but was: ", jsonPath, result.size()));
} }
else if (result.isEmpty()){
return null;
}
return (T) result.get(0); return (T) result.get(0);
} }

Loading…
Cancel
Save