Browse Source

improved tests

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

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

@ -1,5 +1,6 @@
package com.jayway.jsonassert;
import org.hamcrest.Matchers;
import org.junit.Test;
import static com.jayway.jsonassert.JsonAssert.*;
@ -91,4 +92,14 @@ public class JsonAssertTest {
.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()));
}
else if (result.isEmpty()){
return null;
}
return (T) result.get(0);
}

Loading…
Cancel
Save