diff --git a/testing-modules/testing-assertions/pom.xml b/testing-modules/testing-assertions/pom.xml index 09f4291b78..1da53bd77e 100644 --- a/testing-modules/testing-assertions/pom.xml +++ b/testing-modules/testing-assertions/pom.xml @@ -24,6 +24,22 @@ ${commons-collections4.version} test + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + test + + + org.hamcrest + hamcrest + ${hamcrest.version} + test + + + 2.2 + + \ No newline at end of file diff --git a/testing-modules/testing-assertions/src/main/java/com/baeldung/assertequals/Address.java b/testing-modules/testing-assertions/src/main/java/com/baeldung/assertequals/Address.java new file mode 100644 index 0000000000..905dd44af9 --- /dev/null +++ b/testing-modules/testing-assertions/src/main/java/com/baeldung/assertequals/Address.java @@ -0,0 +1,61 @@ +package com.baeldung.assertequals; + +public class Address { + + private Long id; + + private String city; + + private String street; + + private String country; + + public Address(final Long id, final String city, final String street, final String country) { + this.id = id; + this.city = city; + this.street = street; + this.country = country; + } + + public Long getId() { + return id; + } + + public void setId(final Long id) { + this.id = id; + } + + public String getCity() { + return city; + } + + public void setCity(final String city) { + this.city = city; + } + + public String getStreet() { + return street; + } + + public void setStreet(final String street) { + this.street = street; + } + + public String getCountry() { + return country; + } + + public void setCountry(final String country) { + this.country = country; + } + + @Override + public String toString() { + return "Address{" + + "id=" + id + + ", city='" + city + '\'' + + ", street='" + street + '\'' + + ", country='" + country + '\'' + + '}'; + } +} diff --git a/testing-modules/testing-assertions/src/main/java/com/baeldung/assertequals/Person.java b/testing-modules/testing-assertions/src/main/java/com/baeldung/assertequals/Person.java new file mode 100644 index 0000000000..8ea64357ec --- /dev/null +++ b/testing-modules/testing-assertions/src/main/java/com/baeldung/assertequals/Person.java @@ -0,0 +1,60 @@ +package com.baeldung.assertequals; + +public class Person { + + private Long id; + + private String firstName; + + private String lastName; + + private Address address; + + public Person(final Long id, final String firstName, final String lastName) { + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + } + + public Address getAddress() { + return address; + } + + public void setAddress(final Address address) { + this.address = address; + } + + public Long getId() { + return id; + } + + public void setId(final Long id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(final String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(final String lastName) { + this.lastName = lastName; + } + + @Override + public String toString() { + return "Person{" + + "id=" + id + + ", firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + ", address=" + address + + '}'; + } +} diff --git a/testing-modules/testing-assertions/src/test/java/com/baeldung/assertequals/EqualityUnitTest.java b/testing-modules/testing-assertions/src/test/java/com/baeldung/assertequals/EqualityUnitTest.java new file mode 100644 index 0000000000..bf20e02a1d --- /dev/null +++ b/testing-modules/testing-assertions/src/test/java/com/baeldung/assertequals/EqualityUnitTest.java @@ -0,0 +1,96 @@ +package com.baeldung.assertequals; + +import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.ReflectionToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.hamcrest.MatcherAssert; +import org.junit.jupiter.api.Test; +import org.mockito.internal.matchers.apachecommons.ReflectionEquals; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.hamcrest.Matchers.samePropertyValuesAs; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class EqualityUnitTest { + + @Test + void givenPersons_whenUseRecursiveComparison_thenOk() { + Person expected = new Person(1L, "Jane", "Doe"); + Address address1 = new Address(1L, "New York", "Sesame Street", "United States"); + expected.setAddress(address1); + + Person actual = new Person(1L, "Jane", "Doe"); + Address address2 = new Address(1L, "New York", "Sesame Street", "United States"); + actual.setAddress(address2); + + assertThat(actual).usingRecursiveComparison().isEqualTo(expected); + } + + @Test + void givenPersons_whenUseRecursiveComparisonIgnoringField_thenOk() { + Person expected = new Person(1L, "Jane", "Doe"); + Person actual = new Person(2L, "Jane", "Doe"); + + assertThat(actual) + .usingRecursiveComparison() + .ignoringFields("id") + .isEqualTo(expected); + } + + @Test + void givenPersons_whenCheckForSamePropertyValues_thenReturnOk() { + Person expected = new Person(1L, "Jane", "Doe"); + Address address1 = new Address(1L, "New York", "Sesame Street", "United States"); + expected.setAddress(address1); + + Person actual = new Person(1L, "Jane", "Doe"); + Address address2 = new Address(1L, "New York", "Sesame Street", "United States"); + actual.setAddress(address2); + + MatcherAssert.assertThat(actual, samePropertyValuesAs(expected, "address")); + MatcherAssert.assertThat(actual.getAddress(), samePropertyValuesAs(expected.getAddress())); + } + + @Test + void givenPerson_whenReflectionToStringBuilder_thenReturnOk() { + Person expected = new Person(1L, "Jane", "Doe"); + Address address1 = new Address(1L, "New York", "Sesame Street", "United States"); + expected.setAddress(address1); + + Person actual = new Person(1L, "Jane", "Doe"); + Address address2 = new Address(1L, "New York", "Sesame Street", "United States"); + actual.setAddress(address2); + + assertThat(ReflectionToStringBuilder.toString(actual, ToStringStyle.SHORT_PREFIX_STYLE)) + .isEqualTo(ReflectionToStringBuilder.toString(expected, ToStringStyle.SHORT_PREFIX_STYLE)); + } + + @Test + void givenPersons_whenEqualsBuilder_thenReturnTrue() { + Person expected = new Person(1L, "Jane", "Doe"); + Address address1 = new Address(1L, "New York", "Sesame Street", "United States"); + expected.setAddress(address1); + + Person actual = new Person(1L, "Jane", "Doe"); + Address address2 = new Address(1L, "New York", "Sesame Street", "United States"); + actual.setAddress(address2); + + assertTrue(EqualsBuilder.reflectionEquals(expected, actual, "address")); + assertTrue(EqualsBuilder.reflectionEquals(expected.getAddress(), actual.getAddress())); + } + + @Test + void givenPersons_whenReflectionEquals_thenReturnOk() { + Person expected = new Person(1L, "Jane", "Doe"); + Address address1 = new Address(1L, "New York", "Sesame Street", "United States"); + expected.setAddress(address1); + + Person actual = new Person(1L, "Jane", "Doe"); + Address address2 = new Address(1L, "New York", "Sesame Street", "United States"); + actual.setAddress(address2); + + assertTrue(new ReflectionEquals(expected, "address").matches(actual)); + assertTrue(new ReflectionEquals(expected.getAddress()).matches(actual.getAddress())); + } + +}