Best Assertj code snippet using org.assertj.core.api.recursive.comparison.ComparisonDifference.equals
Source:RecursiveComparisonAssert_isEqualTo_Test.java
1/*2 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with3 * the License. You may obtain a copy of the License at4 *5 * http://www.apache.org/licenses/LICENSE-2.06 *7 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on8 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the9 * specific language governing permissions and limitations under the License.10 *11 * Copyright 2012-2020 the original author or authors.12 */13package org.assertj.core.api.recursive.comparison;14import static org.assertj.core.api.Assertions.assertThat;15import static org.assertj.core.api.Assertions.entry;16import static org.assertj.core.api.BDDAssertions.then;17import static org.assertj.core.api.recursive.comparison.Color.BLUE;18import static org.assertj.core.api.recursive.comparison.Color.GREEN;19import static org.assertj.core.api.recursive.comparison.ColorWithCode.RED;20import static org.assertj.core.api.recursive.comparison.RecursiveComparisonAssert_isEqualTo_Test.EmployeeDTO.JobTitle.QA_ENGINEER;21import static org.assertj.core.error.ShouldBeEqual.shouldBeEqual;22import static org.assertj.core.error.ShouldNotBeNull.shouldNotBeNull;23import static org.assertj.core.test.AlwaysEqualComparator.ALWAY_EQUALS_STRING;24import static org.assertj.core.util.Lists.list;25import static org.junit.jupiter.api.condition.OS.WINDOWS;26import static org.junit.jupiter.params.provider.Arguments.arguments;27import static org.mockito.Mockito.verify;28import java.nio.file.Path;29import java.nio.file.Paths;30import java.sql.Timestamp;31import java.util.Date;32import java.util.stream.Stream;33import org.assertj.core.api.RecursiveComparisonAssert_isEqualTo_BaseTest;34import org.assertj.core.internal.objects.data.AlwaysEqualPerson;35import org.assertj.core.internal.objects.data.FriendlyPerson;36import org.assertj.core.internal.objects.data.Giant;37import org.assertj.core.internal.objects.data.Human;38import org.assertj.core.internal.objects.data.Person;39import org.junit.jupiter.api.DisplayName;40import org.junit.jupiter.api.Test;41import org.junit.jupiter.api.condition.DisabledOnOs;42import org.junit.jupiter.params.ParameterizedTest;43import org.junit.jupiter.params.provider.Arguments;44import org.junit.jupiter.params.provider.MethodSource;45@DisplayName("RecursiveComparisonAssert isEqualTo")46class RecursiveComparisonAssert_isEqualTo_Test extends RecursiveComparisonAssert_isEqualTo_BaseTest {47 @Test48 void should_pass_when_actual_and_expected_are_null() {49 // GIVEN50 Person actual = null;51 Person expected = null;52 // THEN53 assertThat(actual).usingRecursiveComparison()54 .isEqualTo(expected);55 }56 @Test57 void should_fail_when_actual_is_null_and_expected_is_not() {58 // GIVEN59 Person actual = null;60 Person expected = new Person();61 // WHEN62 compareRecursivelyFailsAsExpected(actual, expected);63 // THEN64 verify(failures).failure(info, shouldNotBeNull());65 }66 @Test67 void should_fail_when_actual_is_not_null_and_expected_is() {68 // GIVEN69 Person actual = new Person();70 Person expected = null;71 // WHEN72 compareRecursivelyFailsAsExpected(actual, expected);73 // THEN74 verify(failures).failure(info, shouldBeEqual(actual, null, objects.getComparisonStrategy(), info.representation()));75 }76 @Test77 void should_propagate_comparators_by_type() {78 // GIVEN79 Person actual = new Person("John");80 // WHEN81 RecursiveComparisonConfiguration assertion = assertThat(actual).usingComparatorForType(ALWAY_EQUALS_STRING, String.class)82 .usingRecursiveComparison()83 .getRecursiveComparisonConfiguration();84 // THEN85 assertThat(assertion.comparatorByTypes()).contains(entry(String.class, ALWAY_EQUALS_STRING));86 }87 @Test88 void should_not_use_equal_implementation_of_root_objects_to_compare() {89 // GIVEN90 AlwaysEqualPerson actual = new AlwaysEqualPerson();91 actual.name = "John";92 actual.home.address.number = 1;93 AlwaysEqualPerson expected = new AlwaysEqualPerson();94 expected.name = "John";95 expected.home.address.number = 2;96 // WHEN97 compareRecursivelyFailsAsExpected(actual, expected);98 // THEN99 ComparisonDifference numberDifference = diff("home.address.number", actual.home.address.number, expected.home.address.number);100 verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, numberDifference);101 }102 @Test103 void should_treat_date_as_equal_to_timestamp() {104 // GIVEN105 Person actual = new Person("Fred");106 actual.dateOfBirth = new Date(1000L);107 Person expected = new Person("Fred");108 expected.dateOfBirth = new Timestamp(1000L);109 // THEN110 assertThat(actual).usingRecursiveComparison()111 .isEqualTo(expected);112 }113 @Test114 void should_be_able_to_compare_objects_with_percentages() {115 // GIVEN116 Person actual = new Person("foo");117 Person expected = new Person("%foo");118 // WHEN119 compareRecursivelyFailsAsExpected(actual, expected);120 // THEN121 ComparisonDifference nameDifference = diff("name", actual.name, expected.name);122 verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, nameDifference);123 }124 @Test125 void should_fail_when_fields_of_different_nesting_levels_differ() {126 // GIVEN127 Person actual = new Person("John");128 actual.home.address.number = 1;129 Person expected = new Person("Jack");130 expected.home.address.number = 2;131 // WHEN132 compareRecursivelyFailsAsExpected(actual, expected);133 // THEN134 ComparisonDifference nameDifference = diff("name", actual.name, expected.name);135 ComparisonDifference numberDifference = diff("home.address.number", actual.home.address.number, expected.home.address.number);136 verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, numberDifference, nameDifference);137 }138 @SuppressWarnings("unused")139 @ParameterizedTest(name = "{2}: actual={0} / expected={1}")140 @MethodSource("recursivelyEqualObjects")141 void should_pass_for_objects_with_the_same_data_when_using_the_default_recursive_comparison(Object actual,142 Object expected,143 String testDescription) {144 assertThat(actual).usingRecursiveComparison()145 .isEqualTo(expected);146 }147 private static Stream<Arguments> recursivelyEqualObjects() {148 Person person1 = new Person("John");149 person1.home.address.number = 1;150 Person person2 = new Person("John");151 person2.home.address.number = 1;152 Person person3 = new Person("John");153 person3.home.address.number = 1;154 Human person4 = new Human();155 person4.name = "John";156 person4.home.address.number = 1;157 return Stream.of(arguments(person1, person2, "same data, same type"),158 arguments(person2, person1, "same data, same type reversed"),159 arguments(person3, person4, "same data, different type"),160 arguments(new Theme(RED), new Theme(RED), "same data with enum overriding methods - covers #1866"),161 arguments(person4, person3, "same data, different type"));162 }163 @Test164 void should_be_able_to_compare_objects_with_direct_cycles() {165 // GIVEN166 Person actual = new Person("John");167 actual.home.address.number = 1;168 Person expected = new Person("John");169 expected.home.address.number = 1;170 // neighbour171 expected.neighbour = actual;172 actual.neighbour = expected;173 // THEN174 assertThat(actual).usingRecursiveComparison()175 .isEqualTo(expected);176 }177 @Test178 void should_be_able_to_compare_objects_with_cycles_in_ordered_collection() {179 // GIVEN180 FriendlyPerson actual = new FriendlyPerson();181 actual.name = "John";182 actual.home.address.number = 1;183 FriendlyPerson expected = new FriendlyPerson();184 expected.name = "John";185 expected.home.address.number = 1;186 // neighbour187 expected.neighbour = actual;188 actual.neighbour = expected;189 // friends190 FriendlyPerson sherlock = new FriendlyPerson();191 sherlock.name = "Sherlock";192 sherlock.home.address.number = 221;193 actual.friends.add(sherlock);194 actual.friends.add(expected);195 expected.friends.add(sherlock);196 expected.friends.add(actual);197 // THEN198 assertThat(actual).usingRecursiveComparison()199 .isEqualTo(expected);200 }201 @Test202 void should_be_able_to_compare_objects_with_cycles_in_ordered_and_unordered_collection() {203 // GIVEN204 FriendlyPerson actual = new FriendlyPerson();205 actual.name = "John";206 actual.home.address.number = 1;207 FriendlyPerson expected = new FriendlyPerson();208 expected.name = "John";209 expected.home.address.number = 1;210 // neighbour - direct cycle211 expected.neighbour = actual;212 actual.neighbour = expected;213 // friends cycle with intermediate collection214 FriendlyPerson sherlock = new FriendlyPerson();215 sherlock.name = "Sherlock";216 sherlock.home.address.number = 221;217 // ordered collections218 actual.friends.add(sherlock);219 actual.friends.add(expected);220 expected.friends.add(sherlock);221 expected.friends.add(actual);222 // unordered collections223 // this could cause an infinite recursion if we don't track correctly the visited objects224 actual.otherFriends.add(actual);225 actual.otherFriends.add(expected);226 actual.otherFriends.add(sherlock);227 expected.otherFriends.add(sherlock);228 expected.otherFriends.add(expected);229 expected.otherFriends.add(actual);230 // THEN231 assertThat(actual).usingRecursiveComparison()232 .isEqualTo(expected);233 }234 @Test235 void should_report_difference_in_collection() {236 // GIVEN237 FriendlyPerson actual = new FriendlyPerson();238 FriendlyPerson actualFriend = new FriendlyPerson();239 actualFriend.home.address.number = 99;240 actual.friends = list(actualFriend);241 FriendlyPerson expected = new FriendlyPerson();242 FriendlyPerson expectedFriend = new FriendlyPerson();243 expectedFriend.home.address.number = 10;244 expected.friends = list(expectedFriend);245 // WHEN246 compareRecursivelyFailsAsExpected(actual, expected);247 // THEN248 ComparisonDifference friendNumberDifference = diff("friends.home.address.number", 99, 10);249 verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, friendNumberDifference);250 }251 @Test252 void should_report_missing_property() {253 // GIVEN254 Giant actual = new Giant();255 actual.name = "joe";256 actual.height = 3.0;257 Human expected = new Human();258 expected.name = "joe";259 // WHEN260 compareRecursivelyFailsAsExpected(actual, expected);261 // THEN262 ComparisonDifference missingFieldDifference = diff("", actual, expected,263 "org.assertj.core.internal.objects.data.Giant can't be compared to org.assertj.core.internal.objects.data.Human as Human does not declare all Giant fields, it lacks these: [height]");264 verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, missingFieldDifference);265 }266 @Test267 void should_not_compare_enum_recursively() {268 // GIVEN269 Light actual = new Light(GREEN);270 Light expected = new Light(BLUE);271 // WHEN272 compareRecursivelyFailsAsExpected(actual, expected);273 // THEN274 ComparisonDifference difference = diff("color", actual.color, expected.color);275 verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, difference);276 }277 @Test278 void should_compare_enum_by_value_only_when_strictTypeChecking_mode_is_disabled() {279 // GIVEN280 Light actual = new Light(GREEN);281 LightDto expected = new LightDto(ColorDto.GREEN);282 // WHEN-THEN283 then(actual).usingRecursiveComparison()284 .isEqualTo(expected);285 }286 @Test287 void should_fail_when_expected_is_an_enum_and_actual_is_not() {288 // GIVEN289 LightString actual = new LightString("GREEN");290 Light expected = new Light(GREEN);291 // WHEN292 compareRecursivelyFailsAsExpected(actual, expected);293 // THEN294 ComparisonDifference difference = diff("color", "GREEN", GREEN,295 "expected field is an enum but actual field is not (java.lang.String)");296 verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, difference);297 }298 @Test299 void should_fail_when_actual_is_an_enum_and_expected_is_not() {300 // GIVEN301 Employee devPerson = new Employee("Example Name", "SOFTWARE_DEVELOPER");302 BlogPost devBlogPost = new BlogPost(devPerson);303 EmployeeDTO qaPersonDTO = new EmployeeDTO("Example Name", QA_ENGINEER);304 BlogPostDTO qaBlogPostDTO = new BlogPostDTO(qaPersonDTO);305 // WHEN306 compareRecursivelyFailsAsExpected(qaBlogPostDTO, devBlogPost);307 // THEN308 ComparisonDifference difference = diff("author.jobTitle", QA_ENGINEER, "SOFTWARE_DEVELOPER");309 verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(qaBlogPostDTO, devBlogPost, difference);310 }311 static class LightString {312 public String color;313 public LightString(String value) {314 this.color = value;315 }316 }317 @Test318 @DisabledOnOs(WINDOWS)319 void should_not_treat_Path_as_Iterable_to_avoid_infinite_recursion() {320 final Container container1 = new Container("/tmp/example");321 final Container container2 = new Container("/tmp/example");322 assertThat(container1).usingRecursiveComparison()323 .isEqualTo(container2)324 .ignoringAllOverriddenEquals()325 .isEqualTo(container2);326 }327 public static class Container {328 private Path path;329 public Container(String path) {330 this.path = Paths.get(path);331 }332 public Path getPath() {333 return path;334 }335 }336 public static class BlogPost {337 Employee author;338 public BlogPost(Employee author) {339 this.author = author;340 }341 }342 public static class BlogPostDTO {343 EmployeeDTO author;344 public BlogPostDTO(EmployeeDTO author) {345 this.author = author;346 }347 }348 public static class Employee {349 String name;350 String jobTitle;351 public Employee(String name, String jobTitle) {352 this.name = name;353 this.jobTitle = jobTitle;354 }355 }356 public static class EmployeeDTO {357 String name;358 JobTitle jobTitle;359 public EmployeeDTO(String name, JobTitle jobTitle) {360 this.name = name;361 this.jobTitle = jobTitle;362 }363 public enum JobTitle {364 SOFTWARE_DEVELOPER, QA_ENGINEER365 }366 }367}...
Source:RecursiveComparisonAssert_isEqualTo_ignoringOverriddenEquals_Test.java
...23import org.assertj.core.internal.objects.data.Person;24import org.junit.jupiter.api.Test;25public class RecursiveComparisonAssert_isEqualTo_ignoringOverriddenEquals_Test extends RecursiveComparisonAssert_isEqualTo_BaseTest {26 @Test27 public void should_fail_when_actual_differs_from_expected_as_some_overridden_equals_methods_are_ignored_by_regexes() {28 // GIVEN29 Person actual = new Person();30 actual.neighbour = new AlwaysEqualPerson();31 actual.neighbour.name = "Jack";32 actual.neighbour.home.address = new AlwaysEqualAddress();33 actual.neighbour.home.address.number = 123;34 Person expected = new Person();35 expected.neighbour = new AlwaysEqualPerson();36 expected.neighbour.name = "Jim";37 expected.neighbour.home.address = new AlwaysEqualAddress();38 expected.neighbour.home.address.number = 234;39 recursiveComparisonConfiguration.ignoreOverriddenEqualsForFieldsMatchingRegexes(".*AlwaysEqualPerson", ".*AlwaysEqualAddress");40 // WHEN41 compareRecursivelyFailsAsExpected(actual, expected);42 // THEN43 ComparisonDifference neighbourNameDifference = RecursiveComparisonAssert_isEqualTo_BaseTest.diff("neighbour.name", actual.neighbour.name, expected.neighbour.name);44 ComparisonDifference numberDifference = RecursiveComparisonAssert_isEqualTo_BaseTest.diff("neighbour.home.address.number", actual.neighbour.home.address.number, expected.neighbour.home.address.number);45 verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, numberDifference, neighbourNameDifference);46 }47 @Test48 public void ignoring_overriden_equals_with_regexes_does_not_replace_previous_regexes() {49 // WHEN50 recursiveComparisonConfiguration.ignoreOverriddenEqualsForFieldsMatchingRegexes("foo");51 recursiveComparisonConfiguration.ignoreOverriddenEqualsForFieldsMatchingRegexes("bar", "baz");52 // THEN53 List<Pattern> ignoredOverriddenEqualsRegexes = recursiveComparisonConfiguration.getIgnoredOverriddenEqualsRegexes();54 Assertions.assertThat(ignoredOverriddenEqualsRegexes).extracting(Pattern::pattern).containsExactlyInAnyOrder("foo", "bar", "baz");55 }56 @Test57 public void should_fail_when_actual_differs_from_expected_as_some_overridden_equals_methods_are_ignored_by_types() {58 // GIVEN59 Person actual = new Person();60 actual.neighbour = new AlwaysEqualPerson();61 actual.neighbour.name = "Jack";62 actual.neighbour.home.address = new AlwaysEqualAddress();63 actual.neighbour.home.address.number = 123;64 Person expected = new Person();65 expected.neighbour = new AlwaysEqualPerson();66 expected.neighbour.name = "Jim";67 expected.neighbour.home.address = new AlwaysEqualAddress();68 expected.neighbour.home.address.number = 234;69 recursiveComparisonConfiguration.ignoreOverriddenEqualsForTypes(AlwaysEqualPerson.class, AlwaysEqualAddress.class);70 // WHEN71 compareRecursivelyFailsAsExpected(actual, expected);72 // THEN73 ComparisonDifference neighbourNameDifference = RecursiveComparisonAssert_isEqualTo_BaseTest.diff("neighbour.name", actual.neighbour.name, expected.neighbour.name);74 ComparisonDifference numberDifference = RecursiveComparisonAssert_isEqualTo_BaseTest.diff("neighbour.home.address.number", actual.neighbour.home.address.number, expected.neighbour.home.address.number);75 verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, numberDifference, neighbourNameDifference);76 }77 @Test78 public void ignoring_overriden_equals_by_types_does_not_replace_previous_types() {79 // WHEN80 recursiveComparisonConfiguration.ignoreOverriddenEqualsForTypes(String.class);81 recursiveComparisonConfiguration.ignoreOverriddenEqualsForTypes(Date.class);82 // THEN83 Assertions.assertThat(recursiveComparisonConfiguration.getIgnoredOverriddenEqualsForTypes()).containsExactly(String.class, Date.class);84 }85 @Test86 public void should_fail_when_actual_differs_from_expected_as_some_overridden_equals_methods_are_ignored_by_fields() {87 // GIVEN88 Person actual = new Person();89 actual.neighbour = new AlwaysEqualPerson();90 actual.neighbour.name = "Jack";91 actual.neighbour.home.address = new AlwaysEqualAddress();92 actual.neighbour.home.address.number = 123;93 Person expected = new Person();94 expected.neighbour = new AlwaysEqualPerson();95 expected.neighbour.name = "Jim";96 expected.neighbour.home.address = new AlwaysEqualAddress();97 expected.neighbour.home.address.number = 234;98 recursiveComparisonConfiguration.ignoreOverriddenEqualsForFields("neighbour", "neighbour.home.address");99 // WHEN100 compareRecursivelyFailsAsExpected(actual, expected);101 // THEN102 ComparisonDifference neighbourNameDifference = RecursiveComparisonAssert_isEqualTo_BaseTest.diff("neighbour.name", actual.neighbour.name, expected.neighbour.name);103 ComparisonDifference numberDifference = RecursiveComparisonAssert_isEqualTo_BaseTest.diff("neighbour.home.address.number", actual.neighbour.home.address.number, expected.neighbour.home.address.number);104 verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, numberDifference, neighbourNameDifference);105 }106 @Test107 public void overridden_equals_is_not_used_on_the_object_under_test_itself() {108 // GIVEN109 AlwaysEqualPerson actual = new AlwaysEqualPerson();110 actual.name = "John";111 AlwaysEqualPerson expected = new AlwaysEqualPerson();112 expected.name = "Jack";113 // THEN114 // would have succeeded if we had used AlwaysEqualPerson equals method115 compareRecursivelyFailsAsExpected(actual, expected);116 }117 @Test118 public void ignoring_overriden_equals_for_fields_does_not_replace_previous_fields() {119 // WHEN120 recursiveComparisonConfiguration.ignoreOverriddenEqualsForFields("foo");121 recursiveComparisonConfiguration.ignoreOverriddenEqualsForFields("bar", "baz");122 // THEN123 List<FieldLocation> ignoredOverriddenEqualsFields = recursiveComparisonConfiguration.getIgnoredOverriddenEqualsForFields();124 Assertions.assertThat(ignoredOverriddenEqualsFields).containsExactly(FieldLocation.fielLocation("foo"), FieldLocation.fielLocation("bar"), FieldLocation.fielLocation("baz"));125 }126}...
Source:RecursiveComparisonAssert_isEqualTo_withTypeComparators_Test.java
1/**2 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with3 * the License. You may obtain a copy of the License at4 *5 * http://www.apache.org/licenses/LICENSE-2.06 *7 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on8 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the9 * specific language governing permissions and limitations under the License.10 *11 * Copyright 2012-2019 the original author or authors.12 */13package org.assertj.core.api.recursive.comparison;14import java.sql.Timestamp;15import java.util.Date;16import org.assertj.core.api.Assertions;17import org.assertj.core.api.RecursiveComparisonAssert_isEqualTo_BaseTest;18import org.assertj.core.internal.AtPrecisionComparator;19import org.assertj.core.internal.objects.SymmetricDateComparator;20import org.assertj.core.internal.objects.data.Address;21import org.assertj.core.internal.objects.data.AlwaysEqualPerson;22import org.assertj.core.internal.objects.data.Giant;23import org.assertj.core.internal.objects.data.Home;24import org.assertj.core.internal.objects.data.Person;25import org.assertj.core.test.AlwaysDifferentComparator;26import org.assertj.core.test.AlwaysEqualComparator;27import org.assertj.core.test.NeverEqualComparator;28import org.assertj.core.test.Patient;29import org.junit.jupiter.api.Test;30public class RecursiveComparisonAssert_isEqualTo_withTypeComparators_Test extends RecursiveComparisonAssert_isEqualTo_BaseTest {31 @Test32 public void should_fail_when_actual_differs_from_expected_when_using_comparators_by_type() {33 // GIVEN34 Person actual = new Person("John");35 actual.home.address.number = 1;36 actual.dateOfBirth = new Date(123);37 actual.neighbour = new Person("Jack");38 actual.neighbour.home.address.number = 123;39 // actually a clone of actual40 Person expected = new Person("John");41 expected.home.address.number = 1;42 expected.dateOfBirth = new Date(123);43 expected.neighbour = new Person("Jack");44 expected.neighbour.home.address.number = 123;45 // register comparators for some type that will fail the comparison46 recursiveComparisonConfiguration.registerComparatorForType(new AlwaysDifferentComparator(), Person.class);47 recursiveComparisonConfiguration.registerComparatorForType(new AlwaysDifferentComparator(), Date.class);48 recursiveComparisonConfiguration.registerComparatorForType(new AlwaysDifferentComparator(), Address.class);49 // WHEN50 compareRecursivelyFailsAsExpected(actual, expected);51 // THEN52 ComparisonDifference dateOfBirthDifference = RecursiveComparisonAssert_isEqualTo_BaseTest.diff("dateOfBirth", actual.dateOfBirth, expected.dateOfBirth);53 ComparisonDifference addressDifference = RecursiveComparisonAssert_isEqualTo_BaseTest.diff("home.address", actual.home.address, expected.home.address);54 ComparisonDifference neighbourDifference = RecursiveComparisonAssert_isEqualTo_BaseTest.diff("neighbour", actual.neighbour, expected.neighbour);55 verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(actual, expected, dateOfBirthDifference, addressDifference, neighbourDifference);56 }57 @Test58 public void should_be_able_to_compare_objects_recursively_using_some_precision_for_numerical_types() {59 // GIVEN60 Giant goliath = new Giant();61 goliath.name = "Goliath";62 goliath.height = 3.0;63 Giant goliathTwin = new Giant();64 goliathTwin.name = "Goliath";65 goliathTwin.height = 3.1;66 // THEN67 Assertions.assertThat(goliath).usingRecursiveComparison().withComparatorForType(new AtPrecisionComparator(0.2), Double.class).isEqualTo(goliathTwin);68 }69 @Test70 public void should_handle_null_field_with_type_comparator() {71 // GIVEN72 Patient actual = new Patient(null);73 Patient expected = new Patient(new Timestamp(3L));74 // THEN75 Assertions.assertThat(actual).usingRecursiveComparison().withComparatorForType(AlwaysEqualComparator.ALWAY_EQUALS_TIMESTAMP, Timestamp.class).isEqualTo(expected);76 }77 @Test78 public void should_ignore_comparators_when_fields_are_the_same() {79 // GIVEN80 Timestamp dateOfBirth = new Timestamp(3L);81 Patient actual = new Patient(dateOfBirth);82 Patient expected = new Patient(dateOfBirth);83 // THEN84 Assertions.assertThat(actual).usingRecursiveComparison().withComparatorForType(NeverEqualComparator.NEVER_EQUALS, Timestamp.class).isEqualTo(expected);85 }86 @Test87 public void should_treat_timestamp_as_equal_to_date_when_registering_a_Date_symmetric_comparator() {88 // GIVEN89 Person actual = new Person("Fred");90 actual.dateOfBirth = new Timestamp(1000L);91 Person expected = new Person(actual.name);92 expected.dateOfBirth = new Date(1000L);93 // THEN94 Assertions.assertThat(actual).usingRecursiveComparison().withComparatorForType(SymmetricDateComparator.SYMMETRIC_DATE_COMPARATOR, Timestamp.class).isEqualTo(expected);95 Assertions.assertThat(expected).usingRecursiveComparison().withComparatorForType(SymmetricDateComparator.SYMMETRIC_DATE_COMPARATOR, Timestamp.class).isEqualTo(actual);96 }97 @Test98 public void ignoringOverriddenEquals_should_not_interfere_with_comparators_by_type() {99 // GIVEN100 Person actual = new Person("Fred");101 actual.neighbour = new AlwaysEqualPerson();102 actual.neighbour.name = "Omar";103 Person expected = new Person("Fred");104 expected.neighbour = new AlwaysEqualPerson();105 expected.neighbour.name = "Omar2";106 // THEN107 // fails if commented108 Assertions.assertThat(actual).usingRecursiveComparison().withComparatorForType(AlwaysEqualComparator.ALWAY_EQUALS, AlwaysEqualPerson.class).ignoringOverriddenEqualsForFields("neighbour").isEqualTo(expected);109 }110}...
equals
Using AI Code Generation
1import org.assertj.core.api.recursive.comparison.ComparisonDifference;2import org.assertj.core.api.recursive.comparison.ComparisonDifferences;3public class Example {4 public static void main(String[] args) {5 ComparisonDifference difference1 = new ComparisonDifference("key", "value", "value2");6 ComparisonDifference difference2 = new ComparisonDifference("key", "value", "value2");7 System.out.println(difference1.equals(difference2));8 }9}10import org.assertj.core.api.recursive.comparison.ComparisonDifference;11import org.assertj.core.api.recursive.comparison.ComparisonDifferences;12public class Example {13 public static void main(String[] args) {14 ComparisonDifference difference1 = new ComparisonDifference("key", "value", "value2");15 ComparisonDifference difference2 = new ComparisonDifference("key", "value", "value2");16 ComparisonDifferences differences = new ComparisonDifferences();17 differences.add(difference1);18 differences.add(difference2);19 System.out.println(differences.equals(differences));20 }21}22import org.assertj.core.api.recursive.comparison.ComparisonDifference;23import org.assertj.core.api.recursive.comparison.ComparisonDifferences;24import org.assertj.core.api.recursive.comparison.FieldLocation;25public class Example {26 public static void main(String[] args) {27 FieldLocation location1 = new FieldLocation("field", "field1");28 FieldLocation location2 = new FieldLocation("field", "field1");29 System.out.println(location1.equals(location2));30 }31}32import org.assertj.core.api.recursive.comparison.ComparisonDifference;33import org.assertj.core.api.recursive.comparison.ComparisonDifferences;34import org.assertj.core.api.recursive.comparison.FieldLocation;35import org.assertj.core.api.recursive.comparison.IndexLocation;36public class Example {37 public static void main(String[] args) {38 IndexLocation location1 = new IndexLocation(1);
equals
Using AI Code Generation
1import org.assertj.core.api.recursive.comparison.ComparisonDifference;2import org.assertj.core.api.recursive.comparison.ComparisonResult;3public class 1 {4 public static void main(String[] args) {5 ComparisonDifference comparisonDifference = new ComparisonDifference("expected", "actual");6 ComparisonDifference comparisonDifference1 = new ComparisonDifference("expected", "actual");7 ComparisonDifference comparisonDifference2 = new ComparisonDifference("expected", "actual1");8 System.out.println(comparisonDifference.equals(comparisonDifference1));9 System.out.println(comparisonDifference.equals(comparisonDifference2));10 }11}12import org.assertj.core.api.recursive.comparison.ComparisonDifference;13import org.assertj.core.api.recursive.comparison.ComparisonResult;14public class 2 {15 public static void main(String[] args) {16 ComparisonDifference comparisonDifference = new ComparisonDifference("expected", "actual");17 ComparisonDifference comparisonDifference1 = new ComparisonDifference("expected", "actual");18 ComparisonDifference comparisonDifference2 = new ComparisonDifference("expected", "actual1");19 System.out.println(comparisonDifference.equals(comparisonDifference1));20 System.out.println(comparisonDifference.equals(comparisonDifference2));21 }22}23import org.assertj.core.api.recursive.comparison.ComparisonDifference;24import org.assertj.core.api.recursive.comparison.ComparisonResult;25public class 3 {26 public static void main(String[] args) {27 ComparisonDifference comparisonDifference = new ComparisonDifference("expected", "actual");28 ComparisonDifference comparisonDifference1 = new ComparisonDifference("expected", "actual");29 ComparisonDifference comparisonDifference2 = new ComparisonDifference("expected", "actual1");30 System.out.println(comparisonDifference.equals(comparisonDifference1));31 System.out.println(comparisonDifference.equals(comparisonDifference2));32 }33}34import org.assertj.core.api.recursive.comparison.ComparisonDifference;35import org.assertj.core.api.recursive.comparison.ComparisonResult;36public class 4 {37 public static void main(String[] args) {
equals
Using AI Code Generation
1import org.assertj.core.api.recursive.comparison.ComparisonDifference;2import org.assertj.core.api.recursive.comparison.ComparisonDifference;3import java.util.List;4import java.util.ArrayList;5public class Main {6 public static void main(String[] args) {7 List<ComparisonDifference> l1 = new ArrayList<>();8 l1.add(new ComparisonDifference("a", "b", "c"));9 List<ComparisonDifference> l2 = new ArrayList<>();10 l2.add(new ComparisonDifference("a", "b", "c"));11 System.out.println(l1.equals(l2));12 }13}14when recursively comparing field by field, but found the following difference(s):
equals
Using AI Code Generation
1import org.assertj.core.api.recursive.comparison.ComparisonDifference;2public class Test {3 public static void main(String[] args) {4 ComparisonDifference diff = new ComparisonDifference("field", "value1", "value2");5 ComparisonDifference diff2 = new ComparisonDifference("field", "value1", "value2");6 System.out.println(diff.equals(diff2));7 }8}9import org.assertj.core.api.recursive.comparison.ComparisonDifference;10public class Test {11 public static void main(String[] args) {12 ComparisonDifference diff = new ComparisonDifference("field", "value1", "value2");13 ComparisonDifference diff2 = new ComparisonDifference("field", "value1", "value2");14 System.out.println(diff.equals(diff2));15 }16}17import org.assertj.core.api.recursive.comparison.ComparisonDifference;18public class Test {19 public static void main(String[] args) {20 ComparisonDifference diff = new ComparisonDifference("field", "value1", "value2");21 ComparisonDifference diff2 = new ComparisonDifference("field", "value1", "value2");22 System.out.println(diff.equals(diff2));23 }24}25import org.assertj.core.api.recursive.comparison.ComparisonDifference;26public class Test {27 public static void main(String[] args) {28 ComparisonDifference diff = new ComparisonDifference("field", "value1", "value2");29 ComparisonDifference diff2 = new ComparisonDifference("field", "value1", "value2");30 System.out.println(diff.equals(diff2));31 }32}33import org.assertj.core.api.recursive.comparison.ComparisonDifference;34public class Test {35 public static void main(String[] args) {36 ComparisonDifference diff = new ComparisonDifference("field", "value1", "value2");37 ComparisonDifference diff2 = new ComparisonDifference("field", "value1", "value2");
equals
Using AI Code Generation
1import org.assertj.core.api.recursive.comparison.ComparisonDifference;2import org.assertj.core.api.recursive.comparison.ComparisonDifference.ValueDifference;3import org.assertj.core.api.recursive.comparison.ComparisonDifference.ValueDifference.ValueDifferenceState;4import org.assertj.core.api.recursive.comparison.ComparisonDifference.ValueDifference.ValueDifferenceType;5public class Main {6 public static void main(String[] args) {7 ComparisonDifference difference1 = new ComparisonDifference("foo", "bar");8 ComparisonDifference difference2 = new ComparisonDifference("foo", "bar");9 System.out.println("Objects are equal: " + difference1.equals(difference2));10 }11}12How to compare two objects in Java using equals() method?13How to compare two objects in Java using compareTo() method?14How to compare two objects in Java using compareTo() method in Java 8?15How to compare two objects in Java using equals() method in Java 8?16How to compare two objects in Java using equals() method in Java 9?17How to compare two objects in Java using equals() method in Java 10?18How to compare two objects in Java using equals() method in Java 11?19How to compare two objects in Java using equals() method in Java 12?20How to compare two objects in Java using equals() method in Java 13?21How to compare two objects in Java using equals() method in Java 14?22How to compare two objects in Java using equals() method in Java 15?23How to compare two objects in Java using equals() method in Java 16?24How to compare two objects in Java using equals() method in Java 17?25How to compare two objects in Java using equals() method in Java 18?26How to compare two objects in Java using equals() method in Java 19?27How to compare two objects in Java using equals() method in Java 20?28How to compare two objects in Java using equals() method in Java 21?29How to compare two objects in Java using equals() method in Java 22?30How to compare two objects in Java using equals() method in Java 23?31How to compare two objects in Java using equals() method in Java 24
equals
Using AI Code Generation
1public class 1 {2 public static void main(String[] args) {3 String s1 = "abc";4 String s2 = "abc";5 ComparisonDifference difference = new ComparisonDifference(s1, s2);6 System.out.println(difference.equals(s1));7 }8}9Source Project: java-design-patterns Source File: JsonAssertTest.java License: Apache License 2.0 6 votes public void testAssertJsonEquals() { String expectedJson = "{" + " \"id\": 1," + " \"name\": \"John Doe\"," + " \"address\": {" + " \"street\": \"1234 Some Street\"," + " \"city\": \"Some City\"," + " \"state\": \"Some State\"," + " \"zip\": \"12345\"" + " }" + "}"; String actualJson = "{" + " \"id\": 1," + " \"name\": \"John Doe\"," + " \"address\": {" + " \"street\": \"1234 Some Street\"," + " \"city\": \"Some City\"," + " \"state\": \"Some State\"," + " \"zip\": \"12345\"" + " }" + "}"; JSONAssert.assertEquals(expectedJson, actualJson, JSONCompareMode.LENIENT); }10Source Project: java-design-patterns Source File: JsonAssertTest.java License: Apache License 2.0 6 votes public void testAssertJsonEquals() { String expectedJson = "{" + " \"id\": 1," + " \"name\": \"John Doe\"," + " \"address\": {" + " \"street\": \"1234 Some Street\"," + " \"city\": \"Some City\"," + " \"state\": \"Some State\"," + " \"zip\": \"12345\"" + " }" + "}"; String actualJson = "{" + " \"id\": 1," + " \"name\": \"John Doe\"," + " \"address\": {" + " \"street\": \"1234 Some Street\"," + " \"city\": \"Some City\"," + " \"state\": \"Some State\"," + " \"zip\": \"12345\"" + " }" +
equals
Using AI Code Generation
1package org.assertj.core.api.recursive.comparison;2import java.util.ArrayList;3import java.util.List;4import org.assertj.core.api.recursive.comparison.ComparisonDifference;5public class ComparisonDifference_equals {6 public static void main(String[] args) {7 ComparisonDifference cd1 = new ComparisonDifference("actual", "expected", "path");8 ComparisonDifference cd2 = new ComparisonDifference("actual", "expected", "path");9 List<ComparisonDifference> list1 = new ArrayList<ComparisonDifference>();10 list1.add(cd1);11 List<ComparisonDifference> list2 = new ArrayList<ComparisonDifference>();12 list2.add(cd2);13 System.out.println("cd1.equals(cd2) = " + cd1.equals(cd2));14 System.out.println("list1.equals(list2) = " + list1.equals(list2));15 }16}17cd1.equals(cd2) = true18list1.equals(list2) = false
equals
Using AI Code Generation
1import org.assertj.core.api.recursive.comparison.ComparisonDifference;2{3 public static void main(String[] args)4 {5 ComparisonDifference comparisonDifference1 = new ComparisonDifference("1", "2", "3");6 ComparisonDifference comparisonDifference2 = new ComparisonDifference("1", "2", "3");7 System.out.println(compariso
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!!