Skip to content

Commit

Permalink
Avoid NullPointerException describing null value
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Edgar <michael@xlate.io>
  • Loading branch information
MikeEdgar committed Apr 2, 2023
1 parent 7414e90 commit 915c3f0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ private static String describe(Object value) {
} else if (value instanceof JSONObject) {
return "a JSON object";
} else {
return value.toString();
return String.valueOf(value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ public void compareValues(String prefix, Object expectedValue, Object actualValu
if (expectedValue == actualValue) {
return;
}
if ((expectedValue == null && actualValue != null) || (expectedValue != null && actualValue == null)) {
if (expectedValue == null || actualValue == null) {
result.fail(prefix, expectedValue, actualValue);
}
if (areNumbers(expectedValue, actualValue)) {
} else if (areNumbers(expectedValue, actualValue)) {
if (areNotSameDoubles(expectedValue, actualValue)) {
result.fail(prefix, expectedValue, actualValue);
}
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/org/skyscreamer/jsonassert/JSONCompareTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.skyscreamer.jsonassert.JSONCompare.compareJSON;
import static org.skyscreamer.jsonassert.JSONCompareMode.LENIENT;
import static org.skyscreamer.jsonassert.JSONCompareMode.NON_EXTENSIBLE;
import static org.skyscreamer.jsonassert.JSONCompareMode.STRICT;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
Expand Down Expand Up @@ -159,6 +160,32 @@ public void reportsUnmatchedJSONArrayWhereExpectedContainsJSONObjectWithUniqueKe
assertThat(result, failsWithMessage(equalTo("[0] Could not find match for element {\"id\":3}")));
}

@Test
public void reportsUnmatchedJSONArrayWhereExpectedContainsNonnullIntegerButActualContainsNullElement() throws JSONException {
JSONCompareResult result = compareJSON("[ 3 ]", "[ null ]", LENIENT);
assertThat(result, failsWithMessage(equalTo("[]\nExpected: 3\n but none found\n ; " +
"[]\nUnexpected: null\n")));
}

@Test
public void reportsUnmatchedJSONArrayWhereExpectedContainsNullElementButActualContainsNonnullInteger() throws JSONException {
JSONCompareResult result = compareJSON("[ null ]", "[ 3 ]", LENIENT);
assertThat(result, failsWithMessage(equalTo("[]\nExpected: null\n but none found\n ; " +
"[]\nUnexpected: 3\n")));
}

@Test
public void reportsStrictUnmatchedJSONArrayWhereExpectedContainsNonnullIntegerButActualContainsNullElement() throws JSONException {
JSONCompareResult result = compareJSON("[ 3 ]", "[ null ]", STRICT);
assertThat(result, failsWithMessage(equalTo("[0]\nExpected: 3\n got: null\n")));
}

@Test
public void reportsStrictUnmatchedJSONArrayWhereExpectedContainsNullButActualContainsNonnullInteger() throws JSONException {
JSONCompareResult result = compareJSON("[ null ]", "[ 3 ]", STRICT);
assertThat(result, failsWithMessage(equalTo("[0]\nExpected: null\n got: 3\n")));
}

private Matcher<JSONCompareResult> failsWithMessage(final Matcher<String> expectedMessage) {
return new TypeSafeMatcher<JSONCompareResult>() {
@Override
Expand Down

0 comments on commit 915c3f0

Please sign in to comment.