Browse Source

merged pull request

pull/59/head
Kalle Stenflo 10 years ago
parent
commit
9f4f2f2492
  1. 12
      json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java
  2. 19
      json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java

12
json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java

@ -12,10 +12,8 @@ import static org.hamcrest.Matchers.*;
public class JsonAsserterImpl implements JsonAsserter {
private final Object jsonObject;
/**
* Instantiates a new JSONAsserter
*
@ -25,13 +23,19 @@ public class JsonAsserterImpl implements JsonAsserter {
this.jsonObject = jsonObject;
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public <T> JsonAsserter assertThat(String path, Matcher<T> matcher) {
T obj = JsonPath.<T>read(jsonObject, path);
T obj = null;
try {
obj = JsonPath.<T>read(jsonObject, path);
} catch (Exception e) {
throw new AssertionError(String.format("Error reading JSON path [%s]", path), e);
}
if (!matcher.matches(obj)) {
throw new AssertionError(String.format("JSON path [%s] doesn't match.\nExpected:\n%s\nActual:\n%s", path, matcher.toString(), obj));

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

@ -1,10 +1,13 @@
package com.jayway.jsonassert;
import com.jayway.jsonpath.InvalidPathException;
import org.hamcrest.Matchers;
import org.junit.Test;
import java.io.InputStream;
import com.jayway.jsonpath.PathNotFoundException;
import static com.jayway.jsonassert.JsonAssert.*;
import static org.hamcrest.Matchers.*;
@ -73,7 +76,6 @@ public class JsonAssertTest {
}
@Test
public void a_document_can_be_expected_not_to_contain_a_path() throws Exception {
with(JSON).assertNotDefined("$.store.bicycle.cool");
@ -159,13 +161,26 @@ public class JsonAssertTest {
}
@Test(expected = InvalidPathException.class)
@Test(expected = AssertionError.class)
public void assert_that_invalid_path_is_thrown() {
JsonAsserter asserter = JsonAssert.with("{\"foo\":\"bar\"}");
asserter.assertEquals("$foo", "bar");
}
@Test
public void testAssertEqualsInteger() throws Exception {
with(getResourceAsStream("lotto.json")).assertEquals("lotto.winners[0].winnerId", 23);
}
@Test(expected = AssertionError.class)
public void testAssertEqualsIntegerInvalidExpected() throws Exception {
with(getResourceAsStream("lotto.json")).assertEquals("lotto.winners[0].winnerId", 24);
}
@Test(expected = AssertionError.class)
public void testAssertEqualsIntegerInvalidField() throws Exception {
with(getResourceAsStream("lotto.json")).assertEquals("lotto.winners[0].winnerId1", 24);
}
private InputStream getResourceAsStream(String resourceName) {
return getClass().getClassLoader().getResourceAsStream(resourceName);

Loading…
Cancel
Save