Skip to content

Commit d254e7e

Browse files
committed
Add new tests to improve coverage of JSONCompare
1 parent 2a86b83 commit d254e7e

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
<dependency>
6464
<groupId>junit</groupId>
6565
<artifactId>junit</artifactId>
66-
<version>4.0</version>
66+
<version>4.10</version>
6767
<scope>test</scope>
6868
</dependency>
6969
</dependencies>

src/main/java/org/skyscreamer/jsonassert/JSONCompareResult.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,9 @@ private String formatFailureMessage(String field, Object expected, Object actual
135135
message.append("\n");
136136
return message.toString();
137137
}
138+
139+
@Override
140+
public String toString() {
141+
return _message;
142+
}
138143
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package org.skyscreamer.jsonassert;
2+
3+
import static org.hamcrest.core.IsEqual.equalTo;
4+
import static org.junit.Assert.assertThat;
5+
import static org.junit.Assert.assertTrue;
6+
import static org.skyscreamer.jsonassert.JSONCompare.compareJSON;
7+
import static org.skyscreamer.jsonassert.JSONCompareMode.LENIENT;
8+
import static org.skyscreamer.jsonassert.JSONCompareMode.NON_EXTENSIBLE;
9+
10+
import org.hamcrest.Description;
11+
import org.hamcrest.Matcher;
12+
import org.json.JSONException;
13+
import org.junit.Ignore;
14+
import org.junit.Test;
15+
import org.junit.internal.matchers.TypeSafeMatcher;
16+
17+
/**
18+
* Unit tests for {@code JSONCompare}.
19+
*/
20+
public class JSONCompareTest {
21+
@Test
22+
public void reportsWrongSimpleValueCountInUnorderedArray() throws JSONException {
23+
JSONCompareResult result = compareJSON("[5, 5]", "[5, 7]", LENIENT);
24+
assertThat(result, failsWithMessage(equalTo("[]: Expected contains 2 5 actual contains 1 ; []: Contains 7, but not expected")));
25+
}
26+
27+
@Test
28+
public void reportsMissingJSONObjectWithUniqueKeyInUnorderedArray() throws JSONException {
29+
JSONCompareResult result = compareJSON("[{\"id\" : 3}]", "[{\"id\" : 5}]", LENIENT);
30+
assertThat(result, failsWithMessage(equalTo("[]: Expected but did not find object where id=3 ; " +
31+
"[]: Contains object where \" + uniqueKey + \"=\" + id + \", but not expected")));
32+
}
33+
34+
@Test
35+
public void reportsUnmatchedJSONObjectInUnorderedArray() throws JSONException {
36+
JSONCompareResult result = compareJSON("[{\"address\" : {\"street\" : \"Acacia Avenue\"}}]", "[{\"age\" : 23}]", LENIENT);
37+
assertThat(result, failsWithMessage(equalTo("Could not find match for element {\"age\":23}")));
38+
}
39+
40+
@Test
41+
public void succeedsWithNestedJSONObjectsInUnorderedArray() throws JSONException {
42+
assertTrue(compareJSON("[{\"address\" : {\"street\" : \"Acacia Avenue\"}}, 5]", "[5, {\"address\" : {\"street\" : \"Acacia Avenue\"}}]", LENIENT).passed());
43+
}
44+
45+
@Test
46+
public void succeedsWithJSONObjectsWithNonUniqueKeyInUnorderedArray() throws JSONException {
47+
String jsonDocument = "[{\"age\" : 43}, {\"age\" : 43}]";
48+
assertTrue(compareJSON(jsonDocument, jsonDocument, LENIENT).passed());
49+
}
50+
51+
@Test
52+
public void succeedsWithSomeNestedJSONObjectsInUnorderedArray() throws JSONException {
53+
String jsonDocument = "[{\"age\" : 43}, {\"age\" : {\"years\" : 43}}]";
54+
assertTrue(compareJSON(jsonDocument, jsonDocument, LENIENT).passed());
55+
}
56+
57+
@Test
58+
public void reportsUnmatchesIntegerValueInUnorderedArrayContainingJSONObject() throws JSONException {
59+
JSONCompareResult result = compareJSON("[{\"address\" : {\"street\" : \"Acacia Avenue\"}}, 5]", "[{\"address\" : {\"street\" : \"Acacia Avenue\"}}, 2]", LENIENT);
60+
assertThat(result, failsWithMessage(equalTo("Could not find match for element 2")));
61+
}
62+
63+
@Test
64+
@Ignore // currently failing
65+
public void failing() throws JSONException {
66+
compareJSON("[{\"id\": 3}]", "[{}]", NON_EXTENSIBLE);
67+
}
68+
69+
private Matcher<JSONCompareResult> failsWithMessage(final Matcher<String> expectedMessage) {
70+
return new TypeSafeMatcher<JSONCompareResult>() {
71+
@Override
72+
public void describeTo(Description description) {
73+
description.appendText("a failed comparison with message ").appendDescriptionOf(expectedMessage);
74+
}
75+
76+
@Override
77+
public boolean matchesSafely(JSONCompareResult item) {
78+
return item.failed() && expectedMessage.matches(item.getMessage());
79+
}
80+
};
81+
}
82+
}

0 commit comments

Comments
 (0)