|
5 | 5 | import static org.junit.Assert.assertTrue; |
6 | 6 | import static org.skyscreamer.jsonassert.JSONCompare.compareJSON; |
7 | 7 | import static org.skyscreamer.jsonassert.JSONCompareMode.LENIENT; |
| 8 | +import static org.skyscreamer.jsonassert.JSONCompareMode.NON_EXTENSIBLE; |
8 | 9 |
|
9 | 10 | import org.hamcrest.Description; |
10 | 11 | import org.hamcrest.Matcher; |
|
16 | 17 | * Unit tests for {@code JSONCompare}. |
17 | 18 | */ |
18 | 19 | public 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\nExpected: 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("\nExpected: 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("\nExpected: 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("{}", "{\"id\": 3}", NON_EXTENSIBLE); |
| 70 | + assertThat(result, failsWithMessage(equalTo("Strict checking failed. Got but did not expect: 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 | + |
19 | 79 | @Test |
20 | 80 | public void reportsWrongSimpleValueCountInUnorderedArray() throws JSONException { |
21 | 81 | JSONCompareResult result = compareJSON("[5, 5]", "[5, 7]", LENIENT); |
|
0 commit comments