How to use Person method of org.assertj.core.api.recursive.comparison.RecursiveComparisonAssert_isEqualTo_withComparatorsForFieldMatchingRegexes_Test class

Best Assertj code snippet using org.assertj.core.api.recursive.comparison.RecursiveComparisonAssert_isEqualTo_withComparatorsForFieldMatchingRegexes_Test.Person

Source:RecursiveComparisonAssert_isEqualTo_withComparatorsForFieldMatchingRegexes_Test.java Github

copy

Full Screen

...44 .withEqualsForFieldsMatchingRegexes(equals, fieldRegexes)45 .isEqualTo(expected);46 }47 private static Stream<Arguments> recursivelyEqualObjectsWhenUsingFieldComparators() {48 Person person1 = new Person("John", "Doe");49 person1.home.address.number = 1;50 Person person2 = new Person("JoHN", "DoE");51 person2.home.address.number = 1;52 Person person3 = new Person("John", "Doe");53 person3.home.address.number = 1;54 Person person4 = new Person("Jack", "Doe");55 person4.home.address.number = 2;56 Person person5 = new Person("John", "Doe");57 person5.home.address.number = 1;58 person5.dateOfBirth = new Date(123);59 person5.neighbour = new Person("John", "Doe");60 person5.neighbour.home.address.number = 123;61 Person person6 = new Person("John", "Doe");62 person6.home.address.number = 1;63 person6.dateOfBirth = new Date(123);64 person6.neighbour = new Person("Jim", "Doe");65 person6.neighbour.home.address.number = 456;66 Person person7 = new Person("John", "Doe");67 person7.title = "Sir";68 Person person8 = new Person("Jack", "Dough");69 person8.title = "Mr";70 return Stream.of(arguments(person1, person2, STRING_EQUALS, array("name.*name"), "same data except case for strings"),71 arguments(person3, person4, ALWAYS_EQUALS, array(".*first..me", "home.address.number"),72 "same data except for address number and first name"),73 // any neighbour differences should be ignored as we compare persons with ALWAYS_EQUALS74 arguments(person5, person6, ALWAYS_EQUALS, array("neigh.*"), "same data except for neighbour"),75 arguments(person7, person8, ALWAYS_EQUALS, array(".*stname", "t.tle"), "same data except for strings"));76 }77 @Test78 void should_fail_when_actual_differs_from_expected_when_using_BiPredicates_by_fields_matching_regexes() {79 // GIVEN80 Person actual = new Person("John", "Doe");81 actual.home.address.number = 1;82 actual.dateOfBirth = new Date(123);83 actual.neighbour = new Person("Jack", "Doe");84 actual.neighbour.home.address.number = 123;85 // actually a clone of actual86 Person expected = new Person("John", "Doe");87 expected.home.address.number = 1;88 expected.dateOfBirth = new Date(123);89 expected.neighbour = new Person("Jack", "Doe");90 expected.neighbour.home.address.number = 123;91 // register BiPredicates for some fields that will fail the comparison92 recursiveComparisonConfiguration.registerEqualsForFieldsMatchingRegexes(ALWAYS_DIFFERENT, "dateOf.*", "neighbour.ho.*");93 // WHEN94 compareRecursivelyFailsAsExpected(actual, expected);95 // THEN96 ComparisonDifference dateOfBirthDiff = diff("dateOfBirth", actual.dateOfBirth, expected.dateOfBirth);97 ComparisonDifference neighbourAddressDiff = diff("neighbour.home", actual.neighbour.home, expected.neighbour.home);98 verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, dateOfBirthDiff, neighbourAddressDiff);99 }100 @Test101 void should_be_able_to_compare_objects_recursively_using_some_precision_for_numerical_fields() {102 // GIVEN103 Giant goliath = new Giant();104 goliath.name = "Goliath";105 goliath.height = 3.0;106 Giant goliathTwin = new Giant();107 goliathTwin.name = "Goliath";108 goliathTwin.height = 3.1;109 // THEN110 then(goliath).usingRecursiveComparison()111 .withEqualsForFieldsMatchingRegexes((Double d1, Double d2) -> Math.abs(d1 - d2) <= 0.2, "hei...")112 .isEqualTo(goliathTwin);113 }114 @Test115 void should_be_able_to_compare_objects_recursively_using_given_BiPredicate_for_specified_nested_field() {116 // GIVEN117 Giant goliath = new Giant();118 goliath.name = "Goliath";119 goliath.height = 3.0;120 goliath.home.address.number = 1;121 Giant goliathTwin = new Giant();122 goliathTwin.name = "Goliath";123 goliathTwin.height = 3.1;124 goliathTwin.home.address.number = 5;125 // THEN126 then(goliath).usingRecursiveComparison()127 .withEqualsForFieldsMatchingRegexes((Double d1, Double d2) -> Math.abs(d1 - d2) <= 0.2, "height")128 .withEqualsForFieldsMatchingRegexes((Integer d1, Integer d2) -> d1 - d2 <= 10, "home.address.number")129 .isEqualTo(goliathTwin);130 }131 @Test132 void should_handle_null_field_with_BiPredicates_by_fields_matching_regexes() {133 // GIVEN134 Patient actual = new Patient(null);135 Patient expected = new Patient(new Timestamp(3L));136 // THEN137 then(actual).usingRecursiveComparison()138 .withEqualsForFieldsMatchingRegexes(ALWAYS_EQUALS, "dateOfBirth")139 .isEqualTo(expected);140 }141 @Test142 void field_BiPredicate_should_take_precedence_over_type_comparator_whatever_their_order_of_registration() {143 // GIVEN144 Patient actual = new Patient(new Timestamp(1L));145 Patient expected = new Patient(new Timestamp(3L));146 // THEN147 then(actual).usingRecursiveComparison()148 .withComparatorForType(NEVER_EQUALS, Timestamp.class)149 .withEqualsForFieldsMatchingRegexes(ALWAYS_EQUALS, "dateOfBirth")150 .isEqualTo(expected);151 then(actual).usingRecursiveComparison()152 .withEqualsForFieldsMatchingRegexes(ALWAYS_EQUALS, "dateOfBirth")153 .withComparatorForType(NEVER_EQUALS, Timestamp.class)154 .isEqualTo(expected);155 }156 @Test157 void exact_field_location_comparators_should_take_precedence_over_regexes_BiPredicates_matching_field_location_whatever_their_order_of_registration() {158 // GIVEN159 Patient actual = new Patient(new Timestamp(1L));160 Patient expected = new Patient(new Timestamp(3L));161 // THEN162 then(actual).usingRecursiveComparison()163 .withEqualsForFields(ALWAYS_EQUALS, "dateOfBirth")164 .withEqualsForFieldsMatchingRegexes(ALWAYS_DIFFERENT, "dateOfB.*")165 .isEqualTo(expected);166 then(actual).usingRecursiveComparison()167 .withEqualsForFieldsMatchingRegexes(ALWAYS_DIFFERENT, "dateOfBi.*")168 .withEqualsForFields(ALWAYS_EQUALS, "dateOfBirth")169 .isEqualTo(expected);170 }171 @Test172 void biPredicates_matching_field_location_take_precedence_over_overridden_equals() {173 // GIVEN174 Person actual = new Person("Fred", "Flint");175 actual.neighbour = new AlwaysEqualPerson();176 actual.neighbour.name = new Name("Omar", "Sy");177 Person expected = new Person("Fred", "Flint");178 expected.neighbour = new AlwaysEqualPerson();179 expected.neighbour.name = new Name("Omar2", "Sy");180 // THEN181 then(actual).usingRecursiveComparison()182 .withEqualsForFieldsMatchingRegexes(ALWAYS_EQUALS, "neighbour") // fails if commented183 .usingOverriddenEquals()184 .isEqualTo(expected);185 }186 @Test187 void should_use_custom_equal_over_reference_comparison() {188 // GIVEN189 Foo actual = new Foo(1);190 Foo expected = new Foo(1);191 BiPredicate<Integer, Integer> greaterThan = (i1, i2) -> Objects.equals(i1, i2 + 1);192 // WHEN193 AssertionError assertionError = expectAssertionError(() -> assertThat(actual).usingRecursiveComparison()194 .withEqualsForFieldsMatchingRegexes(greaterThan,195 "b..")196 .isEqualTo(expected));197 // THEN198 then(assertionError).hasMessageContainingAll("- the fields matching these regexes were compared with the following comparators",199 " - [b..] -> ");200 }201 private static class Foo {202 @SuppressWarnings("unused")203 private final Integer bar;204 private Foo(Integer bar) {205 this.bar = bar;206 }207 }208 static class Person {209 Date dateOfBirth;210 Name name;211 String title;212 double weight;213 double height;214 Home home = new Home();215 Person neighbour;216 public Person(String firstname, String lastname) {217 this.name = new Name(firstname, lastname);218 }219 public Person() {}220 }221 static class Name {222 final String firstname;223 final String lastname;224 public Name(String firstname, String lastname) {225 this.firstname = firstname;226 this.lastname = lastname;227 }228 @Override229 public String toString() {230 return String.format("Name[firstname=%s, lastname=%s]", this.firstname, this.lastname);231 }232 }233 static class AlwaysEqualPerson extends Person {234 @Override235 public boolean equals(Object o) {236 return true;237 }238 }239}...

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 Assertj automation tests on LambdaTest cloud grid

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

Most used method in RecursiveComparisonAssert_isEqualTo_withComparatorsForFieldMatchingRegexes_Test

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful