Best JSONassert code snippet using org.skyscreamer.jsonassert.JSONAssertTest.testFail
Source:JSONAssertTest.java
...35 testPass("\"Joe\"", "\"Joe\"", STRICT);36 testPass("\"Joe\"", "\"Joe\"", LENIENT);37 testPass("\"Joe\"", "\"Joe\"", NON_EXTENSIBLE);38 testPass("\"Joe\"", "\"Joe\"", STRICT_ORDER);39 testFail("\"Joe\"", "\"Joe1\"", STRICT);40 testFail("\"Joe\"", "\"Joe2\"", LENIENT);41 testFail("\"Joe\"", "\"Joe3\"", NON_EXTENSIBLE);42 testFail("\"Joe\"", "\"Joe4\"", STRICT_ORDER);43 }44 @Test45 public void testNumber() throws JSONException {46 testPass("123", "123", STRICT);47 testPass("123", "123", LENIENT);48 testPass("123", "123", NON_EXTENSIBLE);49 testPass("123", "123", STRICT_ORDER);50 testFail("123", "1231", STRICT);51 testFail("123", "1232", LENIENT);52 testFail("123", "1233", NON_EXTENSIBLE);53 testFail("123", "1234", STRICT_ORDER);54 testPass("0", "0", STRICT);55 testPass("-1", "-1", STRICT);56 testPass("0.1", "0.1", STRICT);57 testPass("1.2e5", "1.2e5", STRICT);58 testPass("20.4e-1", "20.4e-1", STRICT);59 testFail("310.1e-1", "31.01", STRICT); // should fail though numbers are the same?60 }61 @Test62 public void testSimple() throws JSONException {63 testPass("{id:1}", "{id:1}", STRICT);64 testFail("{id:1}", "{id:2}", STRICT);65 testPass("{id:1}", "{id:1}", LENIENT);66 testFail("{id:1}", "{id:2}", LENIENT);67 testPass("{id:1}", "{id:1}", NON_EXTENSIBLE);68 testFail("{id:1}", "{id:2}", NON_EXTENSIBLE);69 testPass("{id:1}", "{id:1}", STRICT_ORDER);70 testFail("{id:1}", "{id:2}", STRICT_ORDER);71 }72 @Test73 public void testSimpleStrict() throws JSONException {74 testPass("{id:1}", "{id:1,name:\"Joe\"}", LENIENT);75 testFail("{id:1}", "{id:1,name:\"Joe\"}", STRICT);76 testPass("{id:1}", "{id:1,name:\"Joe\"}", STRICT_ORDER);77 testFail("{id:1}", "{id:1,name:\"Joe\"}", NON_EXTENSIBLE);78 }79 @Test80 public void testReversed() throws JSONException {81 testPass("{name:\"Joe\",id:1}", "{id:1,name:\"Joe\"}", LENIENT);82 testPass("{name:\"Joe\",id:1}", "{id:1,name:\"Joe\"}", STRICT);83 testPass("{name:\"Joe\",id:1}", "{id:1,name:\"Joe\"}", NON_EXTENSIBLE);84 testPass("{name:\"Joe\",id:1}", "{id:1,name:\"Joe\"}", STRICT_ORDER);85 }86 @Test // Currently JSONAssert assumes JSONObject.87 public void testArray() throws JSONException {88 testPass("[1,2,3]","[1,2,3]", STRICT);89 testPass("[1,2,3]","[1,3,2]", LENIENT);90 testFail("[1,2,3]","[1,3,2]", STRICT);91 testFail("[1,2,3]","[4,5,6]", LENIENT);92 testPass("[1,2,3]","[1,2,3]", STRICT_ORDER);93 testPass("[1,2,3]","[1,3,2]", NON_EXTENSIBLE);94 testFail("[1,2,3]","[1,3,2]", STRICT_ORDER);95 testFail("[1,2,3]","[4,5,6]", NON_EXTENSIBLE);96 }97 @Test98 public void testNested() throws JSONException {99 testPass("{id:1,address:{addr1:\"123 Main\", addr2:null, city:\"Houston\", state:\"TX\"}}",100 "{id:1,address:{addr1:\"123 Main\", addr2:null, city:\"Houston\", state:\"TX\"}}", STRICT);101 testFail("{id:1,address:{addr1:\"123 Main\", addr2:null, city:\"Houston\", state:\"TX\"}}",102 "{id:1,address:{addr1:\"123 Main\", addr2:null, city:\"Austin\", state:\"TX\"}}", STRICT);103 }104 @Test105 public void testVeryNested() throws JSONException {106 testPass("{a:{b:{c:{d:{e:{f:{g:{h:{i:{j:{k:{l:{m:{n:{o:{p:\"blah\"}}}}}}}}}}}}}}}}",107 "{a:{b:{c:{d:{e:{f:{g:{h:{i:{j:{k:{l:{m:{n:{o:{p:\"blah\"}}}}}}}}}}}}}}}}", STRICT);108 testFail("{a:{b:{c:{d:{e:{f:{g:{h:{i:{j:{k:{l:{m:{n:{o:{p:\"blah\"}}}}}}}}}}}}}}}}",109 "{a:{b:{c:{d:{e:{f:{g:{h:{i:{j:{k:{l:{m:{n:{o:{z:\"blah\"}}}}}}}}}}}}}}}}", STRICT);110 }111 @Test112 public void testSimpleArray() throws JSONException {113 testPass("{id:1,pets:[\"dog\",\"cat\",\"fish\"]}", // Exact to exact (strict)114 "{id:1,pets:[\"dog\",\"cat\",\"fish\"]}",115 STRICT);116 testFail("{id:1,pets:[\"dog\",\"cat\",\"fish\"]}", // Out-of-order fails (strict)117 "{id:1,pets:[\"dog\",\"fish\",\"cat\"]}",118 STRICT);119 testPass("{id:1,pets:[\"dog\",\"cat\",\"fish\"]}", // Out-of-order ok120 "{id:1,pets:[\"dog\",\"fish\",\"cat\"]}",121 LENIENT);122 testPass("{id:1,pets:[\"dog\",\"cat\",\"fish\"]}", // Out-of-order ok123 "{id:1,pets:[\"dog\",\"fish\",\"cat\"]}",124 NON_EXTENSIBLE);125 testFail("{id:1,pets:[\"dog\",\"cat\",\"fish\"]}", // Out-of-order fails (strict order)126 "{id:1,pets:[\"dog\",\"fish\",\"cat\"]}",127 STRICT_ORDER);128 testFail("{id:1,pets:[\"dog\",\"cat\",\"fish\"]}", // Mismatch129 "{id:1,pets:[\"dog\",\"cat\",\"bird\"]}",130 STRICT);131 testFail("{id:1,pets:[\"dog\",\"cat\",\"fish\"]}", // Mismatch132 "{id:1,pets:[\"dog\",\"cat\",\"bird\"]}",133 LENIENT);134 testFail("{id:1,pets:[\"dog\",\"cat\",\"fish\"]}", // Mismatch135 "{id:1,pets:[\"dog\",\"cat\",\"bird\"]}",136 STRICT_ORDER);137 testFail("{id:1,pets:[\"dog\",\"cat\",\"fish\"]}", // Mismatch138 "{id:1,pets:[\"dog\",\"cat\",\"bird\"]}",139 NON_EXTENSIBLE);140 }141 @Test142 public void testSimpleMixedArray() throws JSONException {143 testPass("{stuff:[321, \"abc\"]}", "{stuff:[\"abc\", 321]}", LENIENT);144 testFail("{stuff:[321, \"abc\"]}", "{stuff:[\"abc\", 789]}", LENIENT);145 }146 @Test147 public void testComplexMixedStrictArray() throws JSONException {148 testPass("{stuff:[{pet:\"cat\"},{car:\"Ford\"}]}", "{stuff:[{pet:\"cat\"},{car:\"Ford\"}]}", STRICT);149 }150 @Test151 public void testComplexMixedArray() throws JSONException {152 testPass("{stuff:[{pet:\"cat\"},{car:\"Ford\"}]}", "{stuff:[{pet:\"cat\"},{car:\"Ford\"}]}", LENIENT);153 }154 @Test155 public void testComplexArrayNoUniqueID() throws JSONException {156 testPass("{stuff:[{address:{addr1:\"123 Main\"}}, {address:{addr1:\"234 Broad\"}}]}",157 "{stuff:[{address:{addr1:\"123 Main\"}}, {address:{addr1:\"234 Broad\"}}]}",158 LENIENT);159 }160 @Test161 public void testSimpleAndComplexStrictArray() throws JSONException {162 testPass("{stuff:[123,{a:\"b\"}]}", "{stuff:[123,{a:\"b\"}]}", STRICT);163 }164 @Test165 public void testSimpleAndComplexArray() throws JSONException {166 testPass("{stuff:[123,{a:\"b\"}]}", "{stuff:[123,{a:\"b\"}]}", LENIENT);167 }168 @Test169 public void testComplexArray() throws JSONException {170 testPass("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}",171 "{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}",172 STRICT); // Exact to exact (strict)173 testFail("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}",174 "{id:1,name:\"Joe\",friends:[{id:3,name:\"Sue\",pets:[\"fish\",\"bird\"]},{id:2,name:\"Pat\",pets:[\"dog\"]}],pets:[]}",175 STRICT); // Out-of-order fails (strict)176 testFail("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}",177 "{id:1,name:\"Joe\",friends:[{id:3,name:\"Sue\",pets:[\"fish\",\"bird\"]},{id:2,name:\"Pat\",pets:[\"dog\"]}],pets:[]}",178 STRICT_ORDER); // Out-of-order fails (strict order)179 testPass("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}",180 "{id:1,name:\"Joe\",friends:[{id:3,name:\"Sue\",pets:[\"fish\",\"bird\"]},{id:2,name:\"Pat\",pets:[\"dog\"]}],pets:[]}",181 LENIENT); // Out-of-order ok182 testPass("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}",183 "{id:1,name:\"Joe\",friends:[{id:3,name:\"Sue\",pets:[\"fish\",\"bird\"]},{id:2,name:\"Pat\",pets:[\"dog\"]}],pets:[]}",184 NON_EXTENSIBLE); // Out-of-order ok185 testFail("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}",186 "{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"cat\",\"fish\"]}],pets:[]}",187 STRICT); // Mismatch (strict)188 testFail("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}",189 "{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"cat\",\"fish\"]}],pets:[]}",190 LENIENT); // Mismatch191 testFail("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}",192 "{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"cat\",\"fish\"]}],pets:[]}",193 STRICT_ORDER); // Mismatch194 testFail("{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"bird\",\"fish\"]}],pets:[]}",195 "{id:1,name:\"Joe\",friends:[{id:2,name:\"Pat\",pets:[\"dog\"]},{id:3,name:\"Sue\",pets:[\"cat\",\"fish\"]}],pets:[]}",196 NON_EXTENSIBLE); // Mismatch197 }198 @Test199 public void testArrayOfArraysStrict() throws JSONException {200 testPass("{id:1,stuff:[[1,2],[2,3],[],[3,4]]}", "{id:1,stuff:[[1,2],[2,3],[],[3,4]]}", STRICT);201 testFail("{id:1,stuff:[[1,2],[2,3],[3,4],[]]}", "{id:1,stuff:[[1,2],[2,3],[],[3,4]]}", STRICT);202 }203 @Test204 public void testArrayOfArrays() throws JSONException {205 testPass("{id:1,stuff:[[4,3],[3,2],[],[1,2]]}", "{id:1,stuff:[[1,2],[2,3],[],[3,4]]}", LENIENT);206 }207 208 @Test209 public void testLenientArrayRecursion() throws JSONException {210 testPass("[{\"arr\":[5, 2, 1]}]", "[{\"b\":3, \"arr\":[1, 5, 2]}]", LENIENT);211 }212 213 @Test 214 public void testFieldMismatch() throws JSONException {215 JSONCompareResult result = JSONCompare.compareJSON("{name:\"Pat\"}", "{name:\"Sue\"}", STRICT);216 FieldComparisonFailure comparisonFailure = result.getFieldFailures().iterator().next();217 Assert.assertEquals("Pat", comparisonFailure.getExpected());218 Assert.assertEquals("Sue", comparisonFailure.getActual());219 Assert.assertEquals("name", comparisonFailure.getField());220 }221 @Test222 public void testBooleanArray() throws JSONException {223 testPass("[true, false, true, true, false]", "[true, false, true, true, false]", STRICT);224 testPass("[false, true, true, false, true]", "[true, false, true, true, false]", LENIENT);225 testFail("[false, true, true, false, true]", "[true, false, true, true, false]", STRICT);226 testPass("[false, true, true, false, true]", "[true, false, true, true, false]", NON_EXTENSIBLE);227 testFail("[false, true, true, false, true]", "[true, false, true, true, false]", STRICT_ORDER);228 }229 @Test230 public void testNullProperty() throws JSONException {231 testFail("{id:1,name:\"Joe\"}", "{id:1,name:null}", STRICT);232 testFail("{id:1,name:null}", "{id:1,name:\"Joe\"}", STRICT);233 }234 @Test235 public void testIncorrectTypes() throws JSONException {236 testFail("{id:1,name:\"Joe\"}", "{id:1,name:[]}", STRICT);237 testFail("{id:1,name:[]}", "{id:1,name:\"Joe\"}", STRICT);238 }239 @Test240 public void testNullEquality() throws JSONException {241 testPass("{id:1,name:null}", "{id:1,name:null}", STRICT);242 }243 @Test244 public void testExpectedArrayButActualObject() throws JSONException {245 testFail("[1]", "{id:1}", LENIENT);246 }247 @Test248 public void testExpectedObjectButActualArray() throws JSONException {249 testFail("{id:1}", "[1]", LENIENT);250 }251 @Test252 public void testEquivalentIntAndLong() throws JSONException {253 JSONObject expected = new JSONObject();254 JSONObject actual = new JSONObject();255 expected.put("id", Integer.valueOf(12345));256 actual.put("id", Long.valueOf(12345));257 JSONAssert.assertEquals(expected, actual, true);258 JSONAssert.assertEquals(actual, expected, true);259 }260 @Test261 public void testEquivalentIntAndDouble() throws JSONException {262 JSONObject expected = new JSONObject();263 JSONObject actual = new JSONObject();264 expected.put("id", Integer.valueOf(12345));265 actual.put("id", Double.valueOf(12345.0));266 JSONAssert.assertEquals(expected, actual, true);267 JSONAssert.assertEquals(actual, expected, true);268 }269 @Test(expected = AssertionError.class)270 public void testAssertNotEqualsWhenEqualStrict() throws JSONException {271 JSONObject expected = new JSONObject();272 JSONObject actual = new JSONObject();273 expected.put("id", Integer.valueOf(12345));274 actual.put("id", Double.valueOf(12345));275 JSONAssert.assertNotEquals(expected, actual, true);276 }277 @Test(expected = AssertionError.class)278 public void testAssertNotEqualsWhenEqualLenient() throws JSONException {279 JSONObject expected = new JSONObject();280 JSONObject actual = new JSONObject();281 expected.put("id", Integer.valueOf(12345));282 actual.put("id", Double.valueOf(12345));283 JSONAssert.assertNotEquals(expected, actual, false);284 }285 @Test()286 public void testAssertNotEqualsWhenEqualDiffObjectsStrict() throws JSONException {287 JSONObject expected = new JSONObject();288 JSONObject actual = new JSONObject();289 expected.put("id", Integer.valueOf(12345));290 expected.put("name", "Joe");291 actual.put("id", Double.valueOf(12345));292 JSONAssert.assertNotEquals(expected, actual, true);293 }294 @Test(expected = AssertionError.class)295 public void testAssertNotEqualsWhenEqualDiffObjectsLenient() throws JSONException {296 JSONObject expected = new JSONObject();297 JSONObject actual = new JSONObject();298 expected.put("id", Integer.valueOf(12345));299 expected.put("name", "Joe");300 actual.put("name", "Joe");301 actual.put("id", Double.valueOf(12345));302 JSONAssert.assertNotEquals(expected, actual, false);303 }304 @Test()305 public void testAssertNotEqualsWhenDifferentStrict() throws JSONException {306 JSONObject expected = new JSONObject();307 JSONObject actual = new JSONObject();308 expected.put("id", Integer.valueOf(12345));309 actual.put("id", Double.valueOf(12346));310 JSONAssert.assertNotEquals(expected, actual, true);311 }312 @Test()313 public void testAssertNotEqualsWhenDifferentLenient() throws JSONException {314 JSONObject expected = new JSONObject();315 JSONObject actual = new JSONObject();316 expected.put("id", Integer.valueOf(12345));317 actual.put("id", Double.valueOf(12346));318 JSONAssert.assertNotEquals(expected, actual, false);319 }320 @Test()321 public void testAssertNotEqualsString() throws JSONException {322 JSONAssert.assertNotEquals("[1,2,3]", "[1,3,2]", STRICT);323 JSONAssert.assertNotEquals("[1,2,3]", "[1,2,4]", LENIENT);324 JSONAssert.assertNotEquals("[1,2,3]", "[1,3,2]", true);325 JSONAssert.assertNotEquals("[1,2,3]", "[1,2,4]", false);326 }327 328 @Test()329 public void testAssertEqualsString() throws JSONException {330 JSONAssert.assertEquals("[1,2,3]", "[1,2,3]", true);331 JSONAssert.assertEquals("{id:12345}", "{id:12345}", false);332 JSONAssert.assertEquals("{id:12345}", "{id:12345, name:\"john\"}", LENIENT);333 JSONAssert.assertEquals("{id:12345}", "{id:12345}", LENIENT);334 JSONAssert.assertEquals("{id:12345}", "{id:12345, name:\"john\"}", LENIENT);335 }336 @Test()337 public void testAssertNotEqualsStringAndJSONObject() throws JSONException {338 JSONObject actual = new JSONObject();339 actual.put("id", Double.valueOf(12345));340 JSONAssert.assertEquals("{id:12345}", actual, false);341 JSONAssert.assertNotEquals("{id:12346}", actual, false);342 }343 @Test()344 public void testAssertNotEqualsJSONArray() throws JSONException {345 JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3));346 JSONAssert.assertEquals("[1,2,3]", actual, false);347 JSONAssert.assertNotEquals("[1,2,4]", actual, false);348 JSONAssert.assertNotEquals("[1,3,2]", actual, true);349 JSONAssert.assertNotEquals(new JSONArray(Arrays.asList(1, 2, 4)), actual, false);350 JSONAssert.assertNotEquals(new JSONArray(Arrays.asList(1, 3, 2)), actual, true);351 }352 353 @Test354 public void testAssertEqualsStringJSONArrayBooleanWithMessage() throws JSONException {355 JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3));356 JSONAssert.assertEquals("Message", "[1,2,3]", actual, false);357 performAssertEqualsTestForMessageVerification("[1,2,4]", actual, false);358 performAssertEqualsTestForMessageVerification("[1,3,2]", actual, true);359 }360 361 @Test362 public void testAssertEqualsStringJSONArrayCompareModeWithMessage() throws JSONException {363 JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3));364 JSONAssert.assertEquals("Message", "[1,2,3]", actual, LENIENT);365 performAssertEqualsTestForMessageVerification("[1,2,4]", actual, LENIENT);366 performAssertEqualsTestForMessageVerification("[1,3,2]", actual, STRICT);367 }368 @Test369 public void testAssertEqualsJSONArray2BooleanWithMessage() throws JSONException {370 JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3));371 JSONAssert.assertEquals("Message", new JSONArray(Arrays.asList(1, 2, 3)), actual, false);372 performAssertEqualsTestForMessageVerification(new JSONArray(Arrays.asList(1, 2, 4)), actual, false);373 performAssertEqualsTestForMessageVerification(new JSONArray(Arrays.asList(1, 3, 2)), actual, true);374 }375 376 @Test377 public void testAssertEqualsJSONArray2JSONCompareWithMessage() throws JSONException {378 JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3));379 380 JSONAssert.assertEquals("Message", new JSONArray(Arrays.asList(1, 2, 3)), actual, LENIENT);381 performAssertEqualsTestForMessageVerification(new JSONArray(Arrays.asList(1, 2, 4)), actual, LENIENT);382 performAssertEqualsTestForMessageVerification(new JSONArray(Arrays.asList(1, 3, 2)), actual, STRICT);383 }384 385 @Test386 public void testAssertEqualsString2Boolean() throws JSONException {387 JSONAssert.assertEquals("Message", "{id:12345}", "{id:12345}", false);388 JSONAssert.assertEquals("Message", "{id:12345}", "{id:12345, name:\"john\"}", false);389 390 performAssertEqualsTestForMessageVerification("{id:12345}", "{id:12345, name:\"john\"}", true);391 performAssertEqualsTestForMessageVerification("{id:12345}", "{id:123456}", false);392 }393 394 @Test395 public void testAssertEqualsString2JSONCompare() throws JSONException {396 JSONAssert.assertEquals("Message", "{id:12345}", "{id:12345}", LENIENT);397 JSONAssert.assertEquals("Message", "{id:12345}", "{id:12345, name:\"john\"}", LENIENT);398 399 performAssertEqualsTestForMessageVerification("{id:12345}", "{id:12345, name:\"john\"}", STRICT);400 performAssertEqualsTestForMessageVerification("{id:12345}", "{id:123456}", LENIENT);401 }402 403 @Test404 public void testAssertEqualsStringJSONObjectBoolean() throws JSONException {405 JSONObject actual = new JSONObject();406 actual.put("id", Double.valueOf(12345));407 JSONAssert.assertEquals("Message", "{id:12345}", actual, false);408 performAssertEqualsTestForMessageVerification("{id:12346}", actual, false);409 performAssertEqualsTestForMessageVerification("[1,2,3]", "[1,3,2]", true);410 }411 412 @Test413 public void testAssertEqualsStringJSONObjectJSONCompare() throws JSONException {414 JSONObject actual = new JSONObject();415 actual.put("id", Double.valueOf(12345));416 JSONAssert.assertEquals("Message", "{id:12345}", actual, LENIENT);417 performAssertEqualsTestForMessageVerification("{id:12346}", actual, LENIENT);418 performAssertEqualsTestForMessageVerification("[1,2,3]", "[1,3,2]", STRICT);419 }420 421 @Test422 public void testAssertEqualsJSONObject2JSONCompare() throws JSONException {423 JSONObject expected = new JSONObject();424 JSONObject actual = new JSONObject();425 expected.put("id", Integer.valueOf(12345));426 actual.put("name", "Joe");427 actual.put("id", Integer.valueOf(12345));428 JSONAssert.assertEquals("Message", expected, actual, LENIENT);429 430 expected.put("street", "St. Paul");431 performAssertEqualsTestForMessageVerification(expected, actual, LENIENT);432 433 expected = new JSONObject();434 actual = new JSONObject();435 expected.put("id", Integer.valueOf(12345));436 actual.put("id", Double.valueOf(12346));437 performAssertEqualsTestForMessageVerification(expected, actual, STRICT);438 }439 440 @Test441 public void testAssertEqualsJSONObject2Boolean() throws JSONException {442 JSONObject expected = new JSONObject();443 JSONObject actual = new JSONObject();444 expected.put("id", Integer.valueOf(12345));445 actual.put("name", "Joe");446 actual.put("id", Integer.valueOf(12345));447 JSONAssert.assertEquals("Message", expected, actual, false);448 449 expected.put("street", "St. Paul");450 performAssertEqualsTestForMessageVerification(expected, actual, false);451 452 expected = new JSONObject();453 actual = new JSONObject();454 expected.put("id", Integer.valueOf(12345));455 actual.put("id", Double.valueOf(12346));456 performAssertEqualsTestForMessageVerification(expected, actual, true);457 }458 459 @Test460 public void testAssertEqualsString2JsonComparator() throws IllegalArgumentException, JSONException {461 JSONAssert.assertEquals("Message", "{\"entry\":{\"id\":x}}", "{\"entry\":{\"id\":1, \"id\":2}}", 462 new CustomComparator(463 JSONCompareMode.STRICT, 464 new Customization("entry.id", 465 new RegularExpressionValueMatcher<Object>("\\d"))466 ));467 468 performAssertEqualsTestForMessageVerification("{\"entry\":{\"id\":x}}", "{\"entry\":{\"id\":1, \"id\":as}}", 469 new CustomComparator(470 JSONCompareMode.STRICT, 471 new Customization("entry.id", 472 new RegularExpressionValueMatcher<Object>("\\d"))473 ));474 }475 476 @Test477 public void testAssertNotEqualsStringJSONArrayBooleanWithMessage() throws JSONException {478 JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3));479 JSONAssert.assertNotEquals("Message", "[1,4,3]", actual, false);480 JSONAssert.assertNotEquals("Message", "[1,4,3]", actual, true);481 performAssertNotEqualsTestForMessageVerification("[1,3,2]", actual, false);482 performAssertNotEqualsTestForMessageVerification("[1,2,3]", actual, true);483 }484 485 @Test486 public void testAssertNotEqualsStringJSONArrayCompareModeWithMessage() throws JSONException {487 JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3));488 JSONAssert.assertNotEquals("Message", "[1,2,4]", actual, LENIENT);489 JSONAssert.assertNotEquals("Message", "[1,2,4]", actual, STRICT);490 performAssertNotEqualsTestForMessageVerification("[1,3,2]", actual, LENIENT);491 performAssertNotEqualsTestForMessageVerification("[1,2,3]", actual, STRICT);492 }493 494 @Test495 public void testAssertNotEqualsJSONArray2BooleanWithMessage() throws JSONException {496 JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3));497 JSONAssert.assertNotEquals("Message", new JSONArray(Arrays.asList(1, 4, 3)), actual, false);498 performAssertNotEqualsTestForMessageVerification(new JSONArray(Arrays.asList(1, 3, 2)), actual, false);499 performAssertNotEqualsTestForMessageVerification(new JSONArray(Arrays.asList(1, 2, 3)), actual, true);500 }501 502 @Test503 public void testAssertNotEqualsJSONArray2JSONCompareWithMessage() throws JSONException {504 JSONArray actual = new JSONArray(Arrays.asList(1, 2, 3));505 506 JSONAssert.assertNotEquals("Message", new JSONArray(Arrays.asList(1, 4, 3)), actual, LENIENT);507 performAssertNotEqualsTestForMessageVerification(new JSONArray(Arrays.asList(1, 3, 2)), actual, LENIENT);508 performAssertNotEqualsTestForMessageVerification(new JSONArray(Arrays.asList(1, 2, 3)), actual, STRICT);509 }510 511 @Test512 public void testAssertNotEqualsString2Boolean() throws JSONException {513 JSONAssert.assertNotEquals("Message", "{id:12345}", "{id:45}", false);514 JSONAssert.assertNotEquals("Message", "{id:12345}", "{id:345, name:\"john\"}", false);515 516 performAssertNotEqualsTestForMessageVerification("{id:12345}", "{id:12345}", true);517 performAssertNotEqualsTestForMessageVerification("{id:12345}", "{id:12345, name:\"John\"}", false);518 }519 520 @Test521 public void testAssertNotEqualsString2JSONCompare() throws JSONException {522 JSONAssert.assertNotEquals("Message", "{id:12345}", "{id:123}", LENIENT);523 JSONAssert.assertNotEquals("Message", "{id:12345, name:\"John\"}", "{id:12345}", LENIENT);524 525 performAssertNotEqualsTestForMessageVerification("{id:12345}", "{id:12345, name:\"john\"}", LENIENT);526 performAssertNotEqualsTestForMessageVerification("{id:12345}", "{id:12345}", STRICT);527 }528 529 @Test530 public void testAssertNotEqualsStringJSONObjectBoolean() throws JSONException {531 JSONObject actual = new JSONObject();532 actual.put("id", Double.valueOf(12345));533 JSONAssert.assertNotEquals("Message", "{id:1234}", actual, false);534 performAssertNotEqualsTestForMessageVerification("{id:12345}", actual, false);535 performAssertNotEqualsTestForMessageVerification("[1,2,3]", "[1,2,3]", true);536 }537 538 @Test539 public void testAssertNotEqualsStringJSONObjectJSONCompare() throws JSONException {540 JSONObject actual = new JSONObject();541 actual.put("id", Double.valueOf(12345));542 JSONAssert.assertNotEquals("Message", "{id:1234}", actual, LENIENT);543 performAssertNotEqualsTestForMessageVerification("{id:12345}", actual, LENIENT);544 performAssertNotEqualsTestForMessageVerification("[1,2,3]", "[1,2,3]", STRICT);545 }546 547 @Test548 public void testAssertNtEqualsJSONObject2JSONCompare() throws JSONException {549 JSONObject expected = new JSONObject();550 JSONObject actual = new JSONObject();551 expected.put("id", Integer.valueOf(12345));552 actual.put("name", "Joe");553 actual.put("id", Integer.valueOf(123));554 JSONAssert.assertNotEquals("Message", expected, actual, LENIENT);555 556 actual.remove("id");557 actual.put("id", Integer.valueOf(12345));558 performAssertNotEqualsTestForMessageVerification(expected, actual, LENIENT);559 560 expected = new JSONObject();561 actual = new JSONObject();562 expected.put("id", Integer.valueOf(12345));563 actual.put("id", Double.valueOf(12345));564 performAssertNotEqualsTestForMessageVerification(expected, actual, STRICT);565 }566 567 @Test568 public void testAssertNotEqualsJSONObject2Boolean() throws JSONException {569 JSONObject expected = new JSONObject();570 JSONObject actual = new JSONObject();571 expected.put("id", Integer.valueOf(12345));572 actual.put("name", "Joe");573 actual.put("id", Integer.valueOf(123));574 JSONAssert.assertNotEquals("Message", expected, actual, false);575 576 actual.remove("id");577 actual.put("id", Integer.valueOf(12345));578 performAssertNotEqualsTestForMessageVerification(expected, actual, false);579 580 expected = new JSONObject();581 actual = new JSONObject();582 expected.put("id", Integer.valueOf(12345));583 actual.put("id", Double.valueOf(12345));584 performAssertNotEqualsTestForMessageVerification(expected, actual, true);585 }586 587 @Test588 public void testAssertNotEqualsString2JsonComparator() throws IllegalArgumentException, JSONException {589 JSONAssert.assertNotEquals("Message", "{\"entry\":{\"id\":x}}", "{\"entry\":{\"id\":1, \"id\":hh}}", 590 new CustomComparator(591 JSONCompareMode.STRICT, 592 new Customization("entry.id", 593 new RegularExpressionValueMatcher<Object>("\\d"))594 ));595 596 performAssertNotEqualsTestForMessageVerification("{\"entry\":{\"id\":x}}", "{\"entry\":{\"id\":1, \"id\":2}}", 597 new CustomComparator(598 JSONCompareMode.STRICT, 599 new Customization("entry.id", 600 new RegularExpressionValueMatcher<Object>("\\d"))601 ));602 }603 604 private void testPass(String expected, String actual, JSONCompareMode compareMode)605 throws JSONException606 {607 String message = expected + " == " + actual + " (" + compareMode + ")";608 JSONCompareResult result = JSONCompare.compareJSON(expected, actual, compareMode);609 Assert.assertTrue(message + "\n " + result.getMessage(), result.passed());610 }611 private void testFail(String expected, String actual, JSONCompareMode compareMode)612 throws JSONException613 {614 String message = expected + " != " + actual + " (" + compareMode + ")";615 JSONCompareResult result = JSONCompare.compareJSON(expected, actual, compareMode);616 Assert.assertTrue(message, result.failed());617 }618 619 private void performAssertEqualsTestForMessageVerification(620 Object expected, 621 Object actual, 622 Object strictMode) throws JSONException {623 624 String message = "Message";625 String testShouldFailMessage = "The test should fail so that the message in AssertionError could be verified.";...
testFail
Using AI Code Generation
1package org.skyscreamer.jsonassert;2import org.skyscreamer.jsonassert.JSONAssert;3import org.skyscreamer.jsonassert.JSONCompareMode;4import org.skyscreamer.jsonassert.comparator.CustomComparator;5import org.skyscreamer.jsonassert.comparator.JSONComparator;6import java.util.Arrays;7import java.util.Comparator;8public class JSONAssertTest {9 public static void main(String[] args) throws Exception {10 String expected = "{ \"name\": \"foo\", \"value\": \"bar\" }";11 String actual = "{ \"name\": \"foo\", \"value\": \"baz\" }";12 JSONAssert.assertEquals(expected, actual, false);13 JSONAssert.assertEquals(expected, actual, new CustomComparator(JSONCompareMode.LENIENT,14 new Comparator<String>() {15 public int compare(String o1, String o2) {16 return 0;17 }18 }));19 JSONAssert.assertEquals(expected, actual, JSONCompareMode.LENIENT);20 JSONAssert.assertEquals(expected, actual, new JSONComparator(JSONCompareMode.LENIENT) {21 public void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result) {22 result.pass();23 }24 });25 JSONAssert.assertEquals(expected, actual, new JSONComparator(JSONCompareMode.LENIENT) {26 public void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result) {27 result.pass();28 }29 });30 JSONAssert.assertEquals(expected, actual, new JSONComparator(JSONCompareMode.LENIENT) {31 public void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result) {32 result.pass();33 }34 });35 JSONAssert.assertEquals(expected, actual, new JSONComparator(JSONCompareMode.LENIENT) {36 public void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result) {37 result.pass();38 }39 });40 JSONAssert.assertEquals(expected, actual, new JSONComparator(JSONCompareMode.LENIENT) {41 public void compareValues(String prefix, Object expectedValue, Object actualValue, JSONCompareResult result) {42 result.pass();43 }44 });45 JSONAssert.assertEquals(expected, actual, new JSONComparator(JSONCompareMode.LENIENT) {
testFail
Using AI Code Generation
1import org.skyscreamer.jsonassert.JSONAssertTest;2JSONAssertTest test = new JSONAssertTest();3test.testFail();4import org.skyscreamer.jsonassert.JSONAssertTest;5JSONAssertTest test = new JSONAssertTest();6test.testFail();7import org.skyscreamer.jsonassert.JSONAssertTest;8JSONAssertTest test = new JSONAssertTest();9test.testFail();10import org.skyscreamer.jsonassert.JSONAssertTest;11JSONAssertTest test = new JSONAssertTest();12test.testFail();13import org.skyscreamer.jsonassert.JSONAssertTest;14JSONAssertTest test = new JSONAssertTest();15test.testFail();16import org.skyscreamer.jsonassert.JSONAssertTest;17JSONAssertTest test = new JSONAssertTest();18test.testFail();19import org.skyscreamer.jsonassert.JSONAssertTest;20JSONAssertTest test = new JSONAssertTest();21test.testFail();22import org.skyscreamer.jsonassert.JSONAssertTest;23JSONAssertTest test = new JSONAssertTest();24test.testFail();
testFail
Using AI Code Generation
1org.skyscreamer.jsonassert.JSONAssertTest.testFail()2org.skyscreamer.jsonassert.JSONAssertTest.testPass()3org.skyscreamer.jsonassert.JSONAssertTest.testFail()4org.skyscreamer.jsonassert.JSONAssertTest.testPass()5org.skyscreamer.jsonassert.JSONAssertTest.testFail()6org.skyscreamer.jsonassert.JSONAssertTest.testPass()7org.skyscreamer.jsonassert.JSONAssertTest.testFail()8org.skyscreamer.jsonassert.JSONAssertTest.testPass()9org.skyscreamer.jsonassert.JSONAssertTest.testFail()10org.skyscreamer.jsonassert.JSONAssertTest.testPass()
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!