How to use performAssertNotEqualsTestForMessageVerification method of org.skyscreamer.jsonassert.JSONAssertTest class

Best JSONassert code snippet using org.skyscreamer.jsonassert.JSONAssertTest.performAssertNotEqualsTestForMessageVerification

Source:JSONAssertTest.java Github

copy

Full Screen

...477 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.";626 String strictModeMessage = "strictMode must be an instance of JSONCompareMode or Boolean";627 boolean assertEqualsFailed = true;628 if(expected instanceof String && actual instanceof String && strictMode instanceof JSONComparator) {629 try {630 JSONAssert.assertEquals(message, (String) expected, (String) actual, (JSONComparator) strictMode);631 assertEqualsFailed = false;632 fail(testShouldFailMessage); //will throw AssertionError633 } catch (AssertionError ae) {634 handleAssertionError(message, assertEqualsFailed, ae);635 }636 }637 else if(expected instanceof String && actual instanceof JSONArray) {638 try {639 if(strictMode instanceof JSONCompareMode) {640 JSONAssert.assertEquals(message, (String) expected, (JSONArray) actual, (JSONCompareMode) strictMode);641 } else if(strictMode instanceof Boolean) {642 JSONAssert.assertEquals(message, (String) expected, (JSONArray) actual, (Boolean) strictMode);643 } else {644 fail(strictModeMessage);645 }646 assertEqualsFailed = false;647 fail(testShouldFailMessage); //will throw AssertionError648 } catch (AssertionError ae) {649 handleAssertionError(message, assertEqualsFailed, ae);650 }651 } else if(expected instanceof JSONArray && actual instanceof JSONArray) {652 try {653 if(strictMode instanceof JSONCompareMode) {654 JSONAssert.assertEquals(message, (JSONArray) expected, (JSONArray) actual, (JSONCompareMode) strictMode);655 } else if(strictMode instanceof Boolean) {656 JSONAssert.assertEquals(message, (JSONArray) expected, (JSONArray) actual, (Boolean) strictMode);657 } else {658 fail(strictModeMessage);659 }660 assertEqualsFailed = false;661 fail(testShouldFailMessage); //will throw AssertionError662 } catch (AssertionError ae) {663 handleAssertionError(message, assertEqualsFailed, ae);664 }665 } else if(expected instanceof String && actual instanceof String) {666 try {667 if(strictMode instanceof JSONCompareMode) {668 JSONAssert.assertEquals(message, (String) expected, (String) actual, (JSONCompareMode) strictMode);669 } else if(strictMode instanceof Boolean) {670 JSONAssert.assertEquals(message, (String) expected, (String) actual, (Boolean) strictMode);671 } else {672 fail(strictModeMessage);673 }674 assertEqualsFailed = false;675 fail(testShouldFailMessage); //will throw AssertionError676 } catch (AssertionError ae) {677 handleAssertionError(message, assertEqualsFailed, ae);678 }679 } else if(expected instanceof String && actual instanceof JSONObject) {680 try {681 if(strictMode instanceof JSONCompareMode) {682 JSONAssert.assertEquals(message, (String) expected, (JSONObject) actual, (JSONCompareMode) strictMode);683 } else if(strictMode instanceof Boolean) {684 JSONAssert.assertEquals(message, (String) expected, (JSONObject) actual, (Boolean) strictMode);685 } else {686 fail(strictModeMessage);687 }688 assertEqualsFailed = false;689 fail(testShouldFailMessage); //will throw AssertionError690 } catch (AssertionError ae) {691 handleAssertionError(message, assertEqualsFailed, ae);692 }693 } else if(expected instanceof JSONObject && actual instanceof JSONObject) {694 try {695 if(strictMode instanceof JSONCompareMode) {696 JSONAssert.assertEquals(message, (JSONObject) expected, (JSONObject) actual, (JSONCompareMode) strictMode);697 } else if(strictMode instanceof Boolean) {698 JSONAssert.assertEquals(message, (JSONObject) expected, (JSONObject) actual, (Boolean) strictMode);699 } else {700 fail(strictModeMessage);701 }702 assertEqualsFailed = false;703 fail(testShouldFailMessage); //will throw AssertionError704 } catch (AssertionError ae) {705 handleAssertionError(message, assertEqualsFailed, ae);706 }707 } else {708 fail("No overloaded method found to call");709 }710 }711 712 private void performAssertNotEqualsTestForMessageVerification(713 Object expected, 714 Object actual, 715 Object strictMode) 716 throws JSONException {717 718 String message = "Message";719 String testShouldFailMessage = "The test should fail so that the message in AssertionError could be verified.";720 String strictModeMessage = "strictMode must be an instance of JSONCompareMode or Boolean";721 boolean assertEqualsFailed = true;722 if(expected instanceof String && actual instanceof String && strictMode instanceof JSONComparator) {723 try {724 JSONAssert.assertNotEquals(message, (String) expected, (String) actual, (JSONComparator) strictMode);725 assertEqualsFailed = false;726 fail(testShouldFailMessage); //will throw AssertionError...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run JSONassert automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in JSONAssertTest

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful