Best Assertj code snippet using org.assertj.core.api.object.ObjectAssert_isEqualsToComparingFields_Test.Jedi
Source:ObjectAssert_isEqualsToComparingFields_Test.java
...17import org.assertj.core.api.Assertions;18import org.assertj.core.api.ObjectAssertBaseTest;19import org.assertj.core.internal.objects.SymmetricDateComparator;20import org.assertj.core.test.AlwaysEqualComparator;21import org.assertj.core.test.Jedi;22import org.assertj.core.test.NeverEqualComparator;23import org.assertj.core.test.Patient;24import org.assertj.core.test.Person;25import org.assertj.core.test.PersonCaseInsensitiveNameComparator;26import org.junit.jupiter.api.Test;27/**28 * Tests for <code>{@link ObjectAssert#isEqualToComparingFieldByField(Object)}</code>.29 *30 * @author Nicolas Fran?ois31 */32public class ObjectAssert_isEqualsToComparingFields_Test extends ObjectAssertBaseTest {33 private Jedi other = new Jedi("Yoda", "Blue");34 @Test35 public void should_be_able_to_use_a_comparator_for_specified_fields() {36 Jedi actual = new Jedi("Yoda", "green");37 Jedi other = new Jedi("Luke", "green");38 Assertions.assertThat(actual).usingComparatorForFields(AlwaysEqualComparator.ALWAY_EQUALS_STRING, "name").isEqualToComparingFieldByField(other);39 }40 @Test41 public void comparators_for_fields_should_have_precedence_over_comparators_for_types() {42 Comparator<String> comparator = ( o1, o2) -> o1.compareTo(o2);43 Jedi actual = new Jedi("Yoda", "green");44 Jedi other = new Jedi("Luke", "green");45 Assertions.assertThat(actual).usingComparatorForFields(AlwaysEqualComparator.ALWAY_EQUALS_STRING, "name").usingComparatorForType(comparator, String.class).isEqualToComparingFieldByField(other);46 }47 @Test48 public void should_be_able_to_use_a_comparator_for_specified_type() {49 Jedi actual = new Jedi("Yoda", "green");50 Jedi other = new Jedi("Luke", "blue");51 Assertions.assertThat(actual).usingComparatorForType(AlwaysEqualComparator.ALWAY_EQUALS_STRING, String.class).isEqualToComparingFieldByField(other);52 }53 @Test54 public void should_be_able_to_use_a_type_comparator_for_any_of_the_type_subclasses() {55 ObjectAssert_isEqualsToComparingFields_Test.JediMaster yoda1 = new ObjectAssert_isEqualsToComparingFields_Test.JediMaster("Yoda", new Jedi("luke", "Green"));56 ObjectAssert_isEqualsToComparingFields_Test.JediMaster yoda2 = new ObjectAssert_isEqualsToComparingFields_Test.JediMaster("Yoda", new Jedi("LUKE", null));57 // Jedi is a subclass of Person58 Assertions.assertThat(yoda1).usingComparatorForType(new PersonCaseInsensitiveNameComparator(), Person.class).isEqualToComparingFieldByField(yoda2);59 Assertions.assertThat(yoda2).usingComparatorForType(new PersonCaseInsensitiveNameComparator(), Person.class).isEqualToComparingFieldByField(yoda1);60 }61 @Test62 public void should_be_able_to_use_a_date_comparator_for_timestamp() {63 ObjectAssert_isEqualsToComparingFields_Test.JediMaster yoda1 = new ObjectAssert_isEqualsToComparingFields_Test.JediMaster("Yoda", new Jedi("luke", "Green"));64 yoda1.dateOfBirth = new Timestamp(1000L);65 ObjectAssert_isEqualsToComparingFields_Test.JediMaster yoda2 = new ObjectAssert_isEqualsToComparingFields_Test.JediMaster("Yoda", new Jedi("LUKE", null));66 yoda2.dateOfBirth = new Date(1000L);67 // use a date comparator to compare either Date or Timestamp68 Assertions.assertThat(yoda1).usingComparatorForType(new PersonCaseInsensitiveNameComparator(), Person.class).usingComparatorForType(SymmetricDateComparator.SYMMETRIC_DATE_COMPARATOR, Date.class).isEqualToComparingFieldByField(yoda2);69 Assertions.assertThat(yoda2).usingComparatorForType(new PersonCaseInsensitiveNameComparator(), Person.class).usingComparatorForType(SymmetricDateComparator.SYMMETRIC_DATE_COMPARATOR, Date.class).isEqualToComparingFieldByField(yoda1);70 Assertions.assertThat(yoda1).usingComparatorForType(new PersonCaseInsensitiveNameComparator(), Person.class).usingComparatorForType(SymmetricDateComparator.SYMMETRIC_DATE_COMPARATOR, Timestamp.class).isEqualToComparingFieldByField(yoda2);71 Assertions.assertThat(yoda2).usingComparatorForType(new PersonCaseInsensitiveNameComparator(), Person.class).usingComparatorForType(SymmetricDateComparator.SYMMETRIC_DATE_COMPARATOR, Timestamp.class).isEqualToComparingFieldByField(yoda1);72 }73 static class JediMaster {74 private Jedi padawan;75 private String name;76 public Date dateOfBirth;77 JediMaster(String name, Jedi padawan) {78 this.name = name;79 this.padawan = padawan;80 }81 public Jedi getPadawan() {82 return padawan;83 }84 public String getName() {85 return name;86 }87 }88 @Test89 public void should_handle_null_field_with_field_comparator() {90 // GIVEN91 Patient adam = new Patient(null);92 Patient eve = new Patient(new Timestamp(3L));93 // THEN94 Assertions.assertThat(adam).usingComparatorForFields(AlwaysEqualComparator.ALWAY_EQUALS, "dateOfBirth").isEqualToComparingFieldByField(eve);95 }...
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!!