How to use usingOverriddenEquals method of org.assertj.core.api.RecursiveComparisonAssert class

Best Assertj code snippet using org.assertj.core.api.RecursiveComparisonAssert.usingOverriddenEquals

Source:RecursiveComparisonAssert.java Github

copy

Full Screen

...493 /**494 * This method instructs the recursive comparison to compare recursively all fields including the one whose type have overridden equals,495 * <b>except fields with java types</b> (at some point we need to compare something!).496 * <p>497 * Since 3.17.0 this is the default behavior for recursive comparisons, to revert to the previous behavior call {@link #usingOverriddenEquals()}.498 * <p>499 * Example:500 * <pre><code class='java'> public class Person {501 * String name;502 * double height;503 * Home home = new Home();504 * }505 *506 * public class Home {507 * Address address = new Address();508 * }509 *510 * public static class Address {511 * int number;512 * String street;513 *514 * // only compares number, ouch!515 * {@literal @}Override516 * public boolean equals(final Object other) {517 * if (!(other instanceof Address)) return false;518 * Address castOther = (Address) other;519 * return Objects.equals(number, castOther.number);520 * }521 * }522 *523 * Person sherlock = new Person("Sherlock", 1.80);524 * sherlock.home.address.street = "Baker Street";525 * sherlock.home.address.number = 221;526 *527 * Person sherlock2 = new Person("Sherlock", 1.80);528 * sherlock2.home.address.street = "Butcher Street";529 * sherlock2.home.address.number = 221;530 *531 * // Assertion succeeds because:532 * // - overridden equals are used533 * // - Address has overridden equals and does not compare street fields.534 * assertThat(sherlock).usingRecursiveComparison()535 * .usingOverriddenEquals()536 * .isEqualTo(sherlock2);537 *538 * // To avoid using Address overridden equals, don't call usingOverriddenEquals() or call ignoringAllOverriddenEquals()539 * // (calling ignoringAllOverriddenEquals() is actually not required as this is the default behavior).540 * // This assertion fails as it will compare home.address.street fields which differ541 * assertThat(sherlock).usingRecursiveComparison()542 * //.ignoringAllOverriddenEquals() // not needed as this is the default543 * .isEqualTo(sherlock2);</code></pre>544 *545 * @return this {@link RecursiveComparisonAssert} to chain other methods.546 */547 public SELF ignoringAllOverriddenEquals() {548 recursiveComparisonConfiguration.ignoreAllOverriddenEquals();549 return myself;550 }551 /**552 * By default the recursive comparison compare recursively all fields including the ones whose type have overridden equals553 * <b>except fields with java types</b> (at some point we need to compare something!).554 * <p>555 * This method instructs the recursive comparison to use overridden equals.556 * <p>557 * Example:558 * <pre><code class='java'> public class Person {559 * String name;560 * double height;561 * Home home = new Home();562 * }563 *564 * public class Home {565 * Address address = new Address();566 * }567 *568 * public static class Address {569 * int number;570 * String street;571 *572 * // only compares number!573 * {@literal @}Override574 * public boolean equals(final Object other) {575 * if (!(other instanceof Address)) return false;576 * Address castOther = (Address) other;577 * return Objects.equals(number, castOther.number);578 * }579 * }580 *581 * Person sherlock = new Person("Sherlock", 1.80);582 * sherlock.home.address.street = "Baker Street";583 * sherlock.home.address.number = 221;584 *585 * Person sherlock2 = new Person("Sherlock", 1.80);586 * sherlock2.home.address.street = "Butcher Street";587 * sherlock2.home.address.number = 221;588 *589 * // assertion succeeds because Address equals does not compare street fields.590 * assertThat(sherlock).usingRecursiveComparison()591 * .usingOverriddenEquals()592 * .isEqualTo(sherlock2);593 *594 * // Assertion fails because:595 * // - Address equals is not used.596 * // - street fields are compared and differ.597 * assertThat(sherlock).usingRecursiveComparison()598 * .isEqualTo(sherlock2);</code></pre>599 *600 * @return this {@link RecursiveComparisonAssert} to chain other methods.601 * @since 3.17.0602 */603 public SELF usingOverriddenEquals() {604 recursiveComparisonConfiguration.useOverriddenEquals();605 return myself;606 }607 /**608 * In case you have instructed the recursive to use overridden {@code equals} with {@link #usingOverriddenEquals()},609 * this method allows to ignore overridden {@code equals} for the given fields (it adds them to the already registered ones).610 * <p>611 * Since 3.17.0 all overridden {@code equals} so this method is only relevant if you have called {@link #usingOverriddenEquals()} before.612 * <p>613 * Nested fields can be specified by using dots like this: {@code home.address.street}614 * <p>615 * Example:616 * <pre><code class='java'> public class Person {617 * String name;618 * double height;619 * Home home = new Home();620 * }621 *622 * public class Home {623 * Address address = new Address();624 * }625 *626 * public static class Address {627 * int number;628 * String street;629 *630 * // only compares number631 * {@literal @}Override632 * public boolean equals(final Object other) {633 * if (!(other instanceof Address)) return false;634 * Address castOther = (Address) other;635 * return Objects.equals(number, castOther.number);636 * }637 * }638 *639 * Person sherlock = new Person("Sherlock", 1.80);640 * sherlock.home.address.street = "Baker Street";641 * sherlock.home.address.number = 221;642 *643 * Person sherlock2 = new Person("Sherlock", 1.80);644 * sherlock2.home.address.street = "Butcher Street";645 * sherlock2.home.address.number = 221;646 *647 * // Assertion succeeds because:648 * // - overridden equals are used649 * // - Address has overridden equals and does not compare street fields.650 * assertThat(sherlock).usingRecursiveComparison()651 * .usingOverriddenEquals()652 * .isEqualTo(sherlock2);653 *654 * // ignoringOverriddenEqualsForFields force a recursive comparison on the given field655 * // Assertion fails because:656 * // - Address equals is not used.657 * // - street fields are compared and differ.658 * assertThat(sherlock).usingRecursiveComparison()659 * .usingOverriddenEquals()660 * .ignoringOverriddenEqualsForFields("home.address")661 * .isEqualTo(sherlock2);</code></pre>662 *663 * @param fields the fields we want to force a recursive comparison on.664 * @return this {@link RecursiveComparisonAssert} to chain other methods.665 */666 @CheckReturnValue667 public SELF ignoringOverriddenEqualsForFields(String... fields) {668 recursiveComparisonConfiguration.ignoreOverriddenEqualsForFields(fields);669 return myself;670 }671 /**672 * By default the recursive comparison uses overridden {@code equals} methods to compare fields,673 * this method allows to force a recursive comparison for all fields of the given types (it adds them to the already registered ones).674 * <p>675 * Since 3.17.0 all overridden {@code equals} so this method is only relevant if you have called {@link #usingOverriddenEquals()} before.676 * <p>677 * Example:678 * <pre><code class='java'> public class Person {679 * String name;680 * double height;681 * Home home = new Home();682 * }683 *684 * public class Home {685 * Address address = new Address();686 * }687 *688 * public static class Address {689 * int number;690 * String street;691 *692 * // only compares number, ouch!693 * {@literal @}Override694 * public boolean equals(final Object other) {695 * if (!(other instanceof Address)) return false;696 * Address castOther = (Address) other;697 * return Objects.equals(number, castOther.number);698 * }699 * }700 *701 * Person sherlock = new Person("Sherlock", 1.80);702 * sherlock.home.address.street = "Baker Street";703 * sherlock.home.address.number = 221;704 *705 * Person sherlock2 = new Person("Sherlock", 1.80);706 * sherlock2.home.address.street = "Butcher Street";707 * sherlock2.home.address.number = 221;708 *709 * // Assertion succeeds because:710 * // - overridden equals are used711 * // - Address has overridden equals and does not compare street fields.712 * assertThat(sherlock).usingRecursiveComparison()713 * .usingOverriddenEquals()714 * .isEqualTo(sherlock2);715 *716 * // ignoringOverriddenEqualsForTypes force a recursive comparison on the given types.717 * // Assertion fails because:718 * // - Address equals is not used.719 * // - street fields are compared and differ.720 * assertThat(sherlock).usingRecursiveComparison()721 * .usingOverriddenEquals()722 * .ignoringOverriddenEqualsForTypes(Address.class)723 * .isEqualTo(sherlock2);</code></pre>724 *725 * @param types the types we want to force a recursive comparison on.726 * @return this {@link RecursiveComparisonAssert} to chain other methods.727 */728 @CheckReturnValue729 public SELF ignoringOverriddenEqualsForTypes(Class<?>... types) {730 recursiveComparisonConfiguration.ignoreOverriddenEqualsForTypes(types);731 return myself;732 }733 /**734 * In case you have instructed the recursive to use overridden {@code equals} with {@link #usingOverriddenEquals()},735 * this method allows to force a recursive comparison for the fields matching the given regexes (it adds them to the already registered ones).736 * <p>737 * Since 3.17.0 all overridden {@code equals} so this method is only relevant if you have called {@link #usingOverriddenEquals()} before.738 * <p>739 * Nested fields can be specified by using dots like: {@code home\.address\.street} ({@code \} is used to escape740 * dots since they have a special meaning in regexes).741 * <p>742 * Example:743 * <pre><code class='java'> public class Person {744 * String name;745 * double height;746 * Home home = new Home();747 * }748 *749 * public class Home {750 * Address address = new Address();751 * }752 *753 * public static class Address {754 * int number;755 * String street;756 *757 * // only compares number, ouch!758 * {@literal @}Override759 * public boolean equals(final Object other) {760 * if (!(other instanceof Address)) return false;761 * Address castOther = (Address) other;762 * return Objects.equals(number, castOther.number);763 * }764 * }765 *766 * Person sherlock = new Person("Sherlock", 1.80);767 * sherlock.home.address.street = "Baker Street";768 * sherlock.home.address.number = 221;769 *770 * Person sherlock2 = new Person("Sherlock", 1.80);771 * sherlock2.home.address.street = "Butcher Street";772 * sherlock2.home.address.number = 221;773 *774 * // assertion succeeds because overridden equals are used and thus street fields are mot compared775 * assertThat(sherlock).usingRecursiveComparison()776 * .usingOverriddenEquals()777 * .isEqualTo(sherlock2);778 *779 * // ignoringOverriddenEqualsForFields force a recursive comparison on the field matching the regex780 * // now this assertion fails as we expect since the home.address.street fields differ781 * assertThat(sherlock).usingRecursiveComparison()782 * .usingOverriddenEquals()783 * .ignoringOverriddenEqualsForFieldsMatchingRegexes("home.*")784 * .isEqualTo(sherlock2);</code></pre>785 *786 * @param regexes regexes used to specify the fields we want to force a recursive comparison on.787 * @return this {@link RecursiveComparisonAssert} to chain other methods.788 */789 @CheckReturnValue790 public SELF ignoringOverriddenEqualsForFieldsMatchingRegexes(String... regexes) {791 recursiveComparisonConfiguration.ignoreOverriddenEqualsForFieldsMatchingRegexes(regexes);792 return myself;793 }794 /**795 * Makes the recursive comparison to ignore collection order in all fields in the object under test.796 * <p>...

Full Screen

Full Screen

usingOverriddenEquals

Using AI Code Generation

copy

Full Screen

1 public void testUsingOverriddenEquals() {2 Person person = new Person("John", "Doe");3 Person person2 = new Person("John", "Doe");4 assertThat(person).usingRecursiveComparison()5 .usingOverriddenEquals()6 .isEqualTo(person2);7 }8 public void testUsingOverriddenEquals_fails() {9 Person person = new Person("John", "Doe");10 Person person2 = new Person("John", "Doe");11 person2.setAge(18);12 assertThat(person).usingRecursiveComparison()13 .usingOverriddenEquals()14 .isEqualTo(person2);15 }16 public class Person {17 private String name;18 private String surname;19 private int age;20 public Person(String name, String surname) {21 this.name = name;22 this.surname = surname;23 }24 public String getName() {25 return name;26 }27 public String getSurname() {28 return surname;29 }30 public int getAge() {31 return age;32 }33 public void setAge(int age) {34 this.age = age;35 }36 public boolean equals(Object o) {37 if (this == o) return true;38 if (o == null || getClass() != o.getClass()) return false;39 Person person = (Person) o;40 return name.equals(person.name) &&41 surname.equals(person.surname);42 }43 public int hashCode() {44 return Objects.hash(name, surname);45 }46 }47}48 <Person{name='John', surname='Doe', age=0}>49 <Person{name='John', surname='Doe', age=18}>50at org.assertj.core.api.recursive.comparison.RecursiveComparisonAssert_isEqualTo_Test.testUsingOverriddenEquals_fails(RecursiveComparisonAssert_isEqualTo_Test.java:59)

Full Screen

Full Screen

usingOverriddenEquals

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.Assertions.assertThat;2import org.assertj.core.api.RecursiveComparisonAssert;3import org.junit.jupiter.api.Test;4class RecursiveComparisonAssertTest {5 void testUsingOverriddenEquals() {6 Person person1 = new Person("John", "Doe");7 Person person2 = new Person("John", "Doe");8 assertThat(person1).usingRecursiveComparison()9 .ignoringFields("id")10 .isEqualTo(person2);11 }12}13package com.baeldung.recursivecomparisonassertj;14import java.util.Objects;15class Person {16 private String firstName;17 private String lastName;18 public Person(String firstName, String lastName) {19 this.firstName = firstName;20 this.lastName = lastName;21 }22 public String getFirstName() {23 return firstName;24 }25 public void setFirstName(String firstName) {26 this.firstName = firstName;27 }28 public String getLastName() {29 return lastName;30 }31 public void setLastName(String lastName) {32 this.lastName = lastName;33 }34 public boolean equals(Object o) {35 if (this == o) return true;36 if (o == null || getClass() != o.getClass()) return false;37 Person person = (Person) o;38 return Objects.equals(firstName, person.firstName) && Objects.equals(lastName, person.lastName);39 }40 public int hashCode() {41 return Objects.hash(firstName, lastName);42 }43}44at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:39)45at org.junit.jupiter.api.AssertionUtils.failNotEqual(AssertionUtils.java:32)46at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:180)47at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:147)48at org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:115)

Full Screen

Full Screen

usingOverriddenEquals

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.RecursiveComparisonAssert;2import org.assertj.core.api.RecursiveComparisonConfiguration;3import org.assertj.core.api.RecursiveComparisonConfigurationBuilder;4class OverrideEquals {5 static class Person {6 private String name;7 private int age;8 private String address;9 public Person(String name, int age, String address) {10 this.name = name;11 this.age = age;12 this.address = address;13 }14 public boolean equals(Object o) {15 if (this == o) return true;16 if (o == null || getClass() != o.getClass()) return false;17 Person person = (Person) o;18 if (age != person.age) return false;19 return name != null ? name.equals(person.name) : person.name == null;20 }21 public int hashCode() {22 int result = name != null ? name.hashCode() : 0;23 result = 31 * result + age;24 return result;25 }26 }27 public static void main(String[] args) {28 Person person1 = new Person("John", 25, "London");29 Person person2 = new Person("John", 25, "London");30 Person person3 = new Person("John", 26, "London");31 .recursiveComparison()32 .withOverriddenEquals("age")33 .build();34 RecursiveComparisonAssert recursiveComparisonAssert = new RecursiveComparisonAssert(person1);35 recursiveComparisonAssert.usingConfiguration(config).isEqualTo(person2);36 recursiveComparisonAssert.usingConfiguration(config).isEqualTo(person3);37 }38}

Full Screen

Full Screen

usingOverriddenEquals

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.RecursiveComparisonAssert2import org.assertj.core.api.Assertions3def usingOverriddenEquals = { RecursiveComparisonAssert rca ->4 rca.usingOverriddenEquals()5}6import org.assertj.core.api.RecursiveComparisonAssert7import org.assertj.core.api.Assertions8def usingRecursiveComparison = { RecursiveComparisonAssert rca ->9 rca.usingRecursiveComparison()10}11import org.assertj.core.api.RecursiveComparisonAssert12import org.assertj.core.api.Assertions13def isEqualToIgnoringGivenFields = { RecursiveComparisonAssert rca, Object expected, String[] ignoredFields ->14 rca.isEqualToIgnoringGivenFields(expected, ignoredFields)15}16import org.assertj.core.api.RecursiveComparisonAssert17import org.assertj.core.api.Assertions18def isEqualToComparingFieldByFieldRecursively = { RecursiveComparisonAssert rca, Object expected ->19 rca.isEqualToComparingFieldByFieldRecursively(expected)20}21import org.assertj.core.api.RecursiveComparisonAssert22import org.assertj.core.api.Assertions23def isEqualToComparingOnlyGivenFields = { RecursiveComparisonAssert rca, Object expected, String[] fields ->24 rca.isEqualToComparingOnlyGivenFields(expected, fields)25}26import org.assertj.core.api.RecursiveComparisonAssert27import org.assertj.core.api.Assertions28def isEqualToComparingFieldByField = { RecursiveComparisonAssert rca, Object expected ->29 rca.isEqualToComparingFieldByField(expected)30}31import org.assertj.core.api.RecursiveComparisonAssert32import org.assertj.core.api.Assertions33def usingDefaultTypeComparators = { RecursiveComparisonAssert rca ->34 rca.usingDefaultTypeComparators()35}36import org.assertj.core.api.RecursiveComparisonAssert37import org.assertj.core.api.Assertions

Full Screen

Full Screen

usingOverriddenEquals

Using AI Code Generation

copy

Full Screen

1import org.junit.Test;2import org.assertj.core.api.RecursiveComparisonAssert;3import static org.assertj.core.api.Assertions.assertThat;4public class RecursiveComparisonAssertTest {5 public void test() {6 Employee emp1 = new Employee("John", 1000, 1);7 Employee emp2 = new Employee("John", 1000, 1);8 assertThat(emp1).usingRecursiveComparison().isEqualTo(emp2);9 }10}11class Employee {12 private String name;13 private int salary;14 private int id;15 public Employee(String name, int salary, int id) {16 this.name = name;17 this.salary = salary;18 this.id = id;19 }20 public String getName() {21 return name;22 }23 public void setName(String name) {24 this.name = name;25 }26 public int getSalary() {27 return salary;28 }29 public void setSalary(int salary) {30 this.salary = salary;31 }32 public int getId() {33 return id;34 }35 public void setId(int id) {36 this.id = id;37 }38}39The isEqualTo() method compares the two objects recursively by comparing

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful