From f466a0b6c5c651df785c3965b8f0cd60dd46289f Mon Sep 17 00:00:00 2001 From: Sarath Chandra Date: Tue, 25 Jun 2013 16:21:38 -0400 Subject: [PATCH] Add "message" param and print path (that failed) in asserts, the failure (default) messaging is inadequate. It doesnot even print the path that failed. this path should help eloberate the messaging --- .../com/jayway/jsonassert/JsonAsserter.java | 16 ++++++++ .../jsonassert/impl/JsonAsserterImpl.java | 39 ++++++++++++++++++- pom.xml | 2 +- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/json-path-assert/src/main/java/com/jayway/jsonassert/JsonAsserter.java b/json-path-assert/src/main/java/com/jayway/jsonassert/JsonAsserter.java index 9cff5bec..50092234 100644 --- a/json-path-assert/src/main/java/com/jayway/jsonassert/JsonAsserter.java +++ b/json-path-assert/src/main/java/com/jayway/jsonassert/JsonAsserter.java @@ -26,6 +26,15 @@ public interface JsonAsserter { */ JsonAsserter assertThat(String path, Matcher matcher); + /** + * @param path + * @param matcher + * @param message + * @param + * @return + */ + JsonAsserter assertThat(String path, Matcher matcher, String message); + /** * Asserts that object specified by path is equal to the expected value. * If they are not, an AssertionError is thrown with the given message. @@ -37,6 +46,8 @@ public interface JsonAsserter { */ JsonAsserter assertEquals(String path, T expected); + JsonAsserter assertEquals(String path, T expected, String message); + /** * Checks that a path is not defined within a document. If the document contains the * given path, an AssertionError is thrown @@ -46,6 +57,8 @@ public interface JsonAsserter { */ JsonAsserter assertNotDefined(String path); + JsonAsserter assertNotDefined(String path, String message); + /** * Asserts that object specified by path is null. If it is not, an AssertionError @@ -55,6 +68,7 @@ public interface JsonAsserter { * @return this to allow fluent assertion chains */ JsonAsserter assertNull(String path); + JsonAsserter assertNull(String path, String message); /** * Asserts that object specified by path is NOT null. If it is, an AssertionError @@ -65,6 +79,8 @@ public interface JsonAsserter { */ JsonAsserter assertNotNull(String path); + JsonAsserter assertNotNull(String path, String message); + /** * Syntactic sugar to allow chaining assertions with a separating and() statement *

diff --git a/json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java b/json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java index 2342336a..54e86f14 100644 --- a/json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java +++ b/json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java @@ -38,7 +38,19 @@ public class JsonAsserterImpl implements JsonAsserter { T obj = JsonPath.read(jsonObject, path); if (!matcher.matches(obj)) { - throw new AssertionError(String.format("JSON doesn't match.\nExpected:\n%s\nActual:\n%s", matcher.toString(), obj)); + throw new AssertionError(String.format("JSON path [%s] doesn't match.\nExpected:\n%s\nActual:\n%s", path, matcher.toString(), obj)); + } + return this; + } + + /** + * {@inheritDoc} + */ + @SuppressWarnings("unchecked") + public JsonAsserter assertThat(String path, Matcher matcher, String message) { + T obj = JsonPath.read(jsonObject, path); + if (!matcher.matches(obj)) { + throw new AssertionError(String.format("JSON Assert Error: %s\nExpected:\n%s\nActual:\n%s", message, matcher.toString(), obj)); } return this; } @@ -63,6 +75,16 @@ public class JsonAsserterImpl implements JsonAsserter { return this; } + @Override + public JsonAsserter assertNotDefined(String path, String message) { + try { + Object o = JsonPath.read(jsonObject, path); + throw new AssertionError(format("Document contains the path <%s> but was expected not to.", path)); + } catch (InvalidPathException e) { + } + return this; + } + /** * {@inheritDoc} */ @@ -70,6 +92,16 @@ public class JsonAsserterImpl implements JsonAsserter { return assertThat(path, nullValue()); } + @Override + public JsonAsserter assertNull(String path, String message) { + return assertThat(path, nullValue(), message); + } + + @Override + public JsonAsserter assertEquals(String path, T expected, String message) { + return null; //To change body of implemented methods use File | Settings | File Templates. + } + /** * {@inheritDoc} */ @@ -77,6 +109,11 @@ public class JsonAsserterImpl implements JsonAsserter { return assertThat(path, notNullValue()); } + @Override + public JsonAsserter assertNotNull(String path, String message) { + return assertThat(path, notNullValue(), message); + } + /** * {@inheritDoc} */ diff --git a/pom.xml b/pom.xml index e3c9bb00..6fad2e95 100644 --- a/pom.xml +++ b/pom.xml @@ -24,7 +24,7 @@ com.jayway.jsonpath json-path-parent pom - 0.8.2-SNAPSHOT + 0.8.1-HBO https://github.com/jayway/JsonPath json-path-parent-pom Java JsonPath implementation