55import static org .junit .Assert .assertTrue ;
66import static org .skyscreamer .jsonassert .JSONCompare .compareJSON ;
77import static org .skyscreamer .jsonassert .JSONCompareMode .LENIENT ;
8+ import static org .skyscreamer .jsonassert .JSONCompareMode .NON_EXTENSIBLE ;
89
910import org .hamcrest .Description ;
1011import org .hamcrest .Matcher ;
1617 * Unit tests for {@code JSONCompare}.
1718 */
1819public class JSONCompareTest {
20+ @ Test
21+ public void succeedsWithEmptyArrays () throws JSONException {
22+ assertTrue (compareJSON ("[]" , "[]" , LENIENT ).passed ());
23+ }
24+
25+ @ Test
26+ public void reportsArraysOfUnequalLength () throws JSONException {
27+ JSONCompareResult result = compareJSON ("[4]" , "[]" , LENIENT );
28+ assertThat (result , failsWithMessage (equalTo ("[]: Expected 1 values and got 0" )));
29+ }
30+
31+ @ Test
32+ public void reportsArrayMissingExpectedElement () throws JSONException {
33+ JSONCompareResult result = compareJSON ("[4]" , "[7]" , LENIENT );
34+ assertThat (result , failsWithMessage (equalTo ("[]: Expected 4, but not found ; []: Contains 7, but not expected" )));
35+ }
36+
37+ @ Test
38+ public void reportsMismatchedFieldValues () throws JSONException {
39+ JSONCompareResult result = compareJSON ("{\" id\" : 3}" , "{\" id\" : 5}" , LENIENT );
40+ assertThat (result , failsWithMessage (equalTo ("id\n Expected: 3\n got: 5\n " )));
41+ }
42+
43+ @ Test
44+ public void reportsUnexpectedArrayWhenExpectingObject () throws JSONException {
45+ JSONCompareResult result = compareJSON ("{}" , "[]" , LENIENT );
46+ assertThat (result , failsWithMessage (equalTo ("\n Expected: a JSON object\n got: a JSON array\n " )));
47+ }
48+
49+ @ Test
50+ public void reportsUnexpectedObjectWhenExpectingArray () throws JSONException {
51+ JSONCompareResult result = compareJSON ("[]" , "{}" , LENIENT );
52+ assertThat (result , failsWithMessage (equalTo ("\n Expected: a JSON array\n got: a JSON object\n " )));
53+ }
54+
55+ @ Test
56+ public void reportsUnexpectedNull () throws JSONException {
57+ JSONCompareResult result = compareJSON ("{\" id\" : 3}" , "{\" id\" : null}" , LENIENT );
58+ assertThat (result , failsWithMessage (equalTo ("id: expected java.lang.Integer, but got null" )));
59+ }
60+
61+ @ Test
62+ public void reportsUnexpectedNonNull () throws JSONException {
63+ JSONCompareResult result = compareJSON ("{\" id\" : null}" , "{\" id\" : \" abc\" }" , LENIENT );
64+ assertThat (result , failsWithMessage (equalTo ("id: expected null, but got a string" )));
65+ }
66+
67+ @ Test
68+ public void reportsUnexpectedFieldInNonExtensibleMode () throws JSONException {
69+ JSONCompareResult result = compareJSON ("{\" obj\" : {}}" , "{\" obj\" : {\" id\" : 3}}" , NON_EXTENSIBLE );
70+ assertThat (result , failsWithMessage (equalTo ("Got unexpected field: obj.id" )));
71+ }
72+
73+ @ Test
74+ public void reportsMismatchedTypes () throws JSONException {
75+ JSONCompareResult result = compareJSON ("{\" arr\" :[]}" , "{\" arr\" :{}}" , LENIENT );
76+ assertThat (result , failsWithMessage (equalTo ("Values of arr have different types: expected an array, but got an object" )));
77+ }
78+
1979 @ Test
2080 public void reportsWrongSimpleValueCountInUnorderedArray () throws JSONException {
2181 JSONCompareResult result = compareJSON ("[5, 5]" , "[5, 7]" , LENIENT );
22- assertThat (result , failsWithMessage (equalTo ("[]: Expected contains 2 5 actual contains 1 ; []: Contains 7, but not expected" )));
82+ assertThat (result , failsWithMessage (equalTo ("[]: Expected 2 occurrence(s) of 5 but got 1 occurrence(s) ; []: Contains 7, but not expected" )));
2383 }
2484
2585 @ Test
2686 public void reportsMissingJSONObjectWithUniqueKeyInUnorderedArray () throws JSONException {
2787 JSONCompareResult result = compareJSON ("[{\" id\" : 3}]" , "[{\" id\" : 5}]" , LENIENT );
2888 assertThat (result , failsWithMessage (equalTo ("[]: Expected but did not find object where id=3 ; " +
29- "[]: Contains object where \" + uniqueKey + \" = \" + id + \" , but not expected" )));
89+ "[]: Contains object where id=5 , but not expected" )));
3090 }
3191
3292 @ Test
3393 public void reportsUnmatchedJSONObjectInUnorderedArray () throws JSONException {
3494 JSONCompareResult result = compareJSON ("[{\" address\" : {\" street\" : \" Acacia Avenue\" }}]" , "[{\" age\" : 23}]" , LENIENT );
35- assertThat (result , failsWithMessage (equalTo ("Could not find match for element {\" age \" :23 }" )));
95+ assertThat (result , failsWithMessage (equalTo ("[0] Could not find match for element {\" address \" :{ \" street \" : \" Acacia Avenue \" } }" )));
3696 }
3797
3898 @ Test
@@ -55,19 +115,19 @@ public void succeedsWithSomeNestedJSONObjectsInUnorderedArray() throws JSONExcep
55115 @ Test
56116 public void reportsUnmatchesIntegerValueInUnorderedArrayContainingJSONObject () throws JSONException {
57117 JSONCompareResult result = compareJSON ("[{\" address\" : {\" street\" : \" Acacia Avenue\" }}, 5]" , "[{\" address\" : {\" street\" : \" Acacia Avenue\" }}, 2]" , LENIENT );
58- assertThat (result , failsWithMessage (equalTo ("Could not find match for element 2 " )));
118+ assertThat (result , failsWithMessage (equalTo ("[1] Could not find match for element 5 " )));
59119 }
60120
61121 @ Test
62122 public void reportsUnmatchedJSONArrayWhereOnlyExpectedContainsJSONObjectWithUniqueKey () throws JSONException {
63123 JSONCompareResult result = compareJSON ("[{\" id\" : 3}]" , "[{}]" , LENIENT );
64- assertThat (result , failsWithMessage (equalTo ("Could not find match for element {}" )));
124+ assertThat (result , failsWithMessage (equalTo ("[0] Could not find match for element {\" id \" :3 }" )));
65125 }
66126
67127 @ Test
68128 public void reportsUnmatchedJSONArrayWhereExpectedContainsJSONObjectWithUniqueKeyButActualContainsElementOfOtherType () throws JSONException {
69129 JSONCompareResult result = compareJSON ("[{\" id\" : 3}]" , "[5]" , LENIENT );
70- assertThat (result , failsWithMessage (equalTo ("Could not find match for element 5 " )));
130+ assertThat (result , failsWithMessage (equalTo ("[0] Could not find match for element { \" id \" :3} " )));
71131 }
72132
73133 private Matcher <JSONCompareResult > failsWithMessage (final Matcher <String > expectedMessage ) {
0 commit comments