Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid NullPointerException describing null value #172

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) {
Comment on lines +56 to +58
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed extra "always true" non-null checks and do not attempt to fail for other reasons when already failing due to one value being null.

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