BAEL-6432 - Asserting Equality on Two Classes without an Equals Method (#14193)

This commit is contained in:
Ana Peterlić 2023-06-15 17:43:10 +02:00 committed by GitHub
parent 521847c5ad
commit dc80d66ef0
4 changed files with 233 additions and 0 deletions

View File

@ -24,6 +24,22 @@
<version>${commons-collections4.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>${hamcrest.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<hamcrest.version>2.2</hamcrest.version>
</properties>
</project>

View File

@ -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 + '\'' +
'}';
}
}

View File

@ -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 +
'}';
}
}

View File

@ -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()));
}
}