Polish assertion msgs in JsonPathExpectationsHelper

This commit is contained in:
Sam Brannen 2015-08-14 17:52:16 +02:00
parent af8d9eab59
commit d2503340e7
2 changed files with 11 additions and 11 deletions

View File

@ -113,7 +113,7 @@ public class JsonPathExpectationsHelper {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
List actualValueList = (List) actualValue; List actualValueList = (List) actualValue;
if (actualValueList.isEmpty()) { if (actualValueList.isEmpty()) {
fail("No matching value for JSON path \"" + this.expression + "\""); fail("No matching value at JSON path \"" + this.expression + "\"");
} }
if (actualValueList.size() != 1) { if (actualValueList.size() != 1) {
fail("Got a list of values " + actualValue + " instead of the expected single value " + expectedValue); fail("Got a list of values " + actualValue + " instead of the expected single value " + expectedValue);
@ -121,7 +121,7 @@ public class JsonPathExpectationsHelper {
actualValue = actualValueList.get(0); actualValue = actualValueList.get(0);
} }
else if (actualValue != null && expectedValue != null) { else if (actualValue != null && expectedValue != null) {
assertEquals("For JSON path \"" + this.expression + "\", type of value", assertEquals("At JSON path \"" + this.expression + "\", type of value",
expectedValue.getClass().getName(), actualValue.getClass().getName()); expectedValue.getClass().getName(), actualValue.getClass().getName());
} }
assertEquals("JSON path \"" + this.expression + "\"", expectedValue, actualValue); assertEquals("JSON path \"" + this.expression + "\"", expectedValue, actualValue);
@ -135,7 +135,7 @@ public class JsonPathExpectationsHelper {
*/ */
public void assertValueIsString(String content) throws ParseException { public void assertValueIsString(String content) throws ParseException {
Object value = assertExistsAndReturn(content); Object value = assertExistsAndReturn(content);
String reason = "Expected string at JSON path \"" + this.expression + "\" but found " + value; String reason = "Expected a string at JSON path \"" + this.expression + "\" but found: " + value;
assertThat(reason, value, instanceOf(String.class)); assertThat(reason, value, instanceOf(String.class));
} }
@ -147,7 +147,7 @@ public class JsonPathExpectationsHelper {
*/ */
public void assertValueIsBoolean(String content) throws ParseException { public void assertValueIsBoolean(String content) throws ParseException {
Object value = assertExistsAndReturn(content); Object value = assertExistsAndReturn(content);
String reason = "Expected boolean at JSON path \"" + this.expression + "\" but found " + value; String reason = "Expected a boolean at JSON path \"" + this.expression + "\" but found: " + value;
assertThat(reason, value, instanceOf(Boolean.class)); assertThat(reason, value, instanceOf(Boolean.class));
} }
@ -159,7 +159,7 @@ public class JsonPathExpectationsHelper {
*/ */
public void assertValueIsNumber(String content) throws ParseException { public void assertValueIsNumber(String content) throws ParseException {
Object value = assertExistsAndReturn(content); Object value = assertExistsAndReturn(content);
String reason = "Expected number at JSON path \"" + this.expression + "\" but found " + value; String reason = "Expected a number at JSON path \"" + this.expression + "\" but found: " + value;
assertThat(reason, value, instanceOf(Number.class)); assertThat(reason, value, instanceOf(Number.class));
} }
@ -170,7 +170,7 @@ public class JsonPathExpectationsHelper {
*/ */
public void assertValueIsArray(String content) throws ParseException { public void assertValueIsArray(String content) throws ParseException {
Object value = assertExistsAndReturn(content); Object value = assertExistsAndReturn(content);
String reason = "Expected array for JSON path \"" + this.expression + "\" but found " + value; String reason = "Expected an array at JSON path \"" + this.expression + "\" but found: " + value;
assertTrue(reason, value instanceof List); assertTrue(reason, value instanceof List);
} }
@ -182,7 +182,7 @@ public class JsonPathExpectationsHelper {
*/ */
public void assertValueIsMap(String content) throws ParseException { public void assertValueIsMap(String content) throws ParseException {
Object value = assertExistsAndReturn(content); Object value = assertExistsAndReturn(content);
String reason = "Expected map at JSON path \"" + this.expression + "\" but found " + value; String reason = "Expected a map at JSON path \"" + this.expression + "\" but found: " + value;
assertThat(reason, value, instanceOf(Map.class)); assertThat(reason, value, instanceOf(Map.class));
} }
@ -209,7 +209,7 @@ public class JsonPathExpectationsHelper {
catch (AssertionError ex) { catch (AssertionError ex) {
return; return;
} }
String reason = String.format("Expected no value for JSON path: %s but found: %s", this.expression, value); String reason = "Expected no value at JSON path \"" + this.expression + "\" but found: " + value;
if (List.class.isInstance(value)) { if (List.class.isInstance(value)) {
assertTrue(reason, ((List<?>) value).isEmpty()); assertTrue(reason, ((List<?>) value).isEmpty());
} }
@ -219,7 +219,7 @@ public class JsonPathExpectationsHelper {
} }
private Object evaluateJsonPath(String content) throws ParseException { private Object evaluateJsonPath(String content) throws ParseException {
String message = "No value for JSON path \"" + this.expression + "\", exception: "; String message = "No value at JSON path \"" + this.expression + "\", exception: ";
try { try {
return this.jsonPath.read(content); return this.jsonPath.read(content);
} }
@ -236,7 +236,7 @@ public class JsonPathExpectationsHelper {
private Object assertExistsAndReturn(String content) throws ParseException { private Object assertExistsAndReturn(String content) throws ParseException {
Object value = evaluateJsonPath(content); Object value = evaluateJsonPath(content);
String reason = "No value for JSON path \"" + this.expression + "\""; String reason = "No value at JSON path \"" + this.expression + "\"";
assertTrue(reason, value != null); assertTrue(reason, value != null);
return value; return value;
} }

View File

@ -64,7 +64,7 @@ public class JsonPathExpectationsHelperTests {
@Test @Test
public void assertValueWithDifferentExpectedType() throws Exception { public void assertValueWithDifferentExpectedType() throws Exception {
exception.expect(AssertionError.class); exception.expect(AssertionError.class);
exception.expectMessage(equalTo("For JSON path \"$.nr\", type of value expected:<java.lang.String> but was:<java.lang.Integer>")); exception.expectMessage(equalTo("At JSON path \"$.nr\", type of value expected:<java.lang.String> but was:<java.lang.Integer>"));
new JsonPathExpectationsHelper("$.nr").assertValue(CONTENT, "5"); new JsonPathExpectationsHelper("$.nr").assertValue(CONTENT, "5");
} }