Best Assertj code snippet using org.assertj.core.api.recursive.comparison.RecursiveComparisonAssert_isEqualTo_Test.Employee
Source:RecursiveComparisonAssert_isEqualTo_Test.java
...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}...
Employee
Using AI Code Generation
1@DisplayName("RecursiveComparisonAssert#isEqualTo(Object)")2class RecursiveComparisonAssert_isEqualTo_Test {3 @DisplayName("should pass when comparing two equal objects")4 void should_pass_when_comparing_two_equal_objects() {5 Employee employee = new Employee("John", "Doe", 35);6 Employee otherEmployee = new Employee("John", "Doe", 35);7 assertThat(employee).usingRecursiveComparison()8 .isEqualTo(otherEmployee);9 }10 @DisplayName("should fail when comparing two different objects")11 void should_fail_when_comparing_two_different_objects() {12 Employee employee = new Employee("John", "Doe", 35);13 Employee otherEmployee = new Employee("Jane", "Doe", 35);14 AssertionError error = expectAssertionError(() -> assertThat(employee).usingRecursiveComparison()15 .isEqualTo(otherEmployee));16 then(error).hasMessage(shouldHaveSameClassAs(actual, otherEmployee).create());17 }18 @DisplayName("should fail when comparing two different objects with different types")19 void should_fail_when_comparing_two_different_objects_with_different_types() {20 Employee employee = new Employee("John", "Doe", 35);21 Object otherEmployee = new Object();22 AssertionError error = expectAssertionError(() -> assertThat(employee).usingRecursiveComparison()23 .isEqualTo(otherEmployee));24 then(error).hasMessage(shouldHaveSameClassAs(actual, otherEmployee).create());25 }26 @DisplayName("should fail when comparing two different objects with different types")27 void should_fail_when_comparing_two_different_objects_with_different_types() {28 Employee employee = new Employee("John", "Doe", 35);29 Object otherEmployee = new Object();30 AssertionError error = expectAssertionError(() -> assertThat(employee).usingRecursiveComparison()31 .isEqualTo(otherEmployee));32 then(error).hasMessage(shouldHaveSameClassAs(actual, otherEmployee).create());33 }34 @DisplayName("should fail when comparing two different objects with different types")
Employee
Using AI Code Generation
1 public void test() {2 assertThat(new Employee("John", "Doe", 30, new Address("London", "UK")))3 .usingRecursiveComparison()4 .isEqualTo(new Employee("John", "Doe", 30, new Address("London", "UK")));5 }6}7 public void test() {8 assertThat(new Employee("John", "Doe", 30, new Address("London", "UK")))9 .usingRecursiveComparison()10 .ignoringFields("age")11 .isEqualTo(new Employee("John", "Doe", 30, new Address("London", "UK")));12 }13 public void test() {14 assertThat(new Employee("John", "Doe", 30, new Address("London", "UK")))15 .usingRecursiveComparison()
Employee
Using AI Code Generation
1public class Foo {2 private Bar bar;3 private Baz baz;4 private List<Baz> bazzes;5 private List<Bar> bars;6}7public class FooTest {8 public void testFoo() {9 Foo foo = new Foo();10 foo.setBar(new Bar());11 foo.setBaz(new Baz());12 foo.setBazzes(new ArrayList<Baz>());13 foo.setBars(new ArrayList<Bar>());14 }15}16-> at com.example.demo.UserControllerTest.getAllUsers(UserControllerTest.java:39)17 someMethod(anyObject(), "raw String");18 someMethod(anyObject(), eq("String by matcher"));19at com.example.demo.UserControllerTest.getAllUsers(UserControllerTest.java:39)20@RunWith(SpringRunner.class)21@WebMvcTest(UserController.class)22public class UserControllerTest {23 private MockMvc mvc;24 private UserService userService;25 public void getAllUsers() throws Exception {26 List<User> users = new ArrayList<>();27 users.add(new User(1, "user1", "
Employee
Using AI Code Generation
1public class EmployeeBuilder {2 private String name;3 private int age;4 private String address;5 public EmployeeBuilder() {6 super();7 }8 public EmployeeBuilder name(String name) {9 this.name = name;10 return this;11 }12 public EmployeeBuilder age(int age) {13 this.age = age;14 return this;15 }16 public EmployeeBuilder address(String address) {17 this.address = address;18 return this;19 }20 public Employee build() {21 return new Employee(name, age, address);22 }23}24public class Employee {25 private String name;26 private int age;27 private String address;28 public Employee(String name, int age, String address) {29 super();30 this.name = name;31 this.age = age;32 this.address = address;33 }34 public String getName() {35 return name;36 }37 public int getAge() {38 return age;39 }40 public String getAddress() {41 return address;42 }43}44public class RecursiveComparisonAssert_isEqualTo_Test {45 public void givenTwoObjectsWithDifferentFields_whenAssertingEquality_thenCorrect() {46 Employee john = new EmployeeBuilder().name("John").age(30).address("London").build();47 Employee jane = new EmployeeBuilder().name("Jane").age(30).address("London").build();48 assertThat(john).usingRecursiveComparison()49 .ignoringFields("name")50 .isEqualTo(jane);51 }52}53when recursively comparing field by field, but found the following 1 difference(s):54public class Employee {55 private String name;56 private int age;57 private String address;58 public boolean equals(Object o
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!!