Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions testing-modules/assertJ/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@
<artifactId>assertJ</artifactId>
<version>0.1-SNAPSHOT</version>
<name>assertJ</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>21</source>
<target>21</target>
</configuration>
</plugin>
</plugins>
</build>

<parent>
<groupId>com.baeldung</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,47 @@
package com.baeldung.assertj.ignoring;

import java.time.LocalDate;

public class Employee {

public Long id;
public String name;
public String department;
public String homeAddress;
public String workAddress;
public LocalDate dateOfBirth;
public Double grossSalary;
public Double netSalary;
public Double grossSalary;

public Employee() {
}

public Employee(Long id, String name, String department, String workAddress, Double netSalary, Double grossSalary) {
this.id = id;
this.name = name;
this.department = department;
this.workAddress = workAddress;
this.netSalary = netSalary;
this.grossSalary = grossSalary;
}

// Optional: Override equals and hashCode for better comparison in some contexts
@Override
public boolean equals(Object o) {
if (this == o)
if (this == o) {
return true;
if (o == null || getClass() != o.getClass())
}
if (o == null || getClass() != o.getClass()) {
return false;
}

Employee employee = (Employee) o;

if (id != null ? !id.equals(employee.id) : employee.id != null)
return false;
if (name != null ? !name.equals(employee.name) : employee.name != null)
return false;
if (department != null ? !department.equals(employee.department) : employee.department != null)
return false;
if (homeAddress != null ? !homeAddress.equals(employee.homeAddress) : employee.homeAddress != null)
return false;
if (workAddress != null ? !workAddress.equals(employee.workAddress) : employee.workAddress != null)
if (id != null ? !id.equals(employee.id) : employee.id != null) {
return false;
if (dateOfBirth != null ? !dateOfBirth.equals(employee.dateOfBirth) : employee.dateOfBirth != null)
}
if (name != null ? !name.equals(employee.name) : employee.name != null) {
return false;
if (grossSalary != null ? !grossSalary.equals(employee.grossSalary) : employee.grossSalary != null)
}
if (department != null ? !department.equals(employee.department) : employee.department != null) {
return false;
}
return netSalary != null ? netSalary.equals(employee.netSalary) : employee.netSalary == null;
}

Expand All @@ -44,10 +50,7 @@ public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (department != null ? department.hashCode() : 0);
result = 31 * result + (homeAddress != null ? homeAddress.hashCode() : 0);
result = 31 * result + (workAddress != null ? workAddress.hashCode() : 0);
result = 31 * result + (dateOfBirth != null ? dateOfBirth.hashCode() : 0);
result = 31 * result + (grossSalary != null ? grossSalary.hashCode() : 0);
result = 31 * result + (netSalary != null ? netSalary.hashCode() : 0);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,97 +1,59 @@
package com.baeldung.assertj.ignoring;

import java.time.LocalDate;
import java.util.List;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

public class EmployeeUnitTest {

@Test
public void givenEmployeesWithDifferentAddresses_whenComparingIgnoringSpecificFields_thenEmployeesAreEqual() {
public void givenEmployeesWithDifferentFields_whenComparingIgnoringSpecificFields_thenEmployeesAreEqual() {

Employee employee1 = new Employee();
employee1.id = 1L;
employee1.name = "John Doe";
employee1.department = "Engineering";
employee1.homeAddress = "123 Home St";
employee1.workAddress = "456 Work Ave";
employee1.dateOfBirth = LocalDate.of(1990, 1, 1);
employee1.grossSalary = 100000.0;
employee1.netSalary = 75000.0;

Employee employee2 = new Employee();
employee2.id = 2L;
employee2.name = "John Doe";
employee2.department = "Engineering";
employee2.homeAddress = "789 Home St";
employee2.workAddress = "101 Work Ave";
employee2.dateOfBirth = LocalDate.of(1990, 1, 1);
employee2.grossSalary = 110000.0;
employee2.netSalary = 80000.0;
Employee employee1 = new Employee(1L, "John Doe", "Engineering", "456 Work Ave", 7500.0, 10000.0);
Employee employee2 = new Employee(2L, "Alan Turing", "Engineering", "101 Work Ave", 8000.0, 11000.0);

Assertions.assertThat(employee1)
.usingRecursiveComparison()
.ignoringFields("id", "homeAddress", "workAddress", "grossSalary", "netSalary")
.isEqualTo(employee2);
.usingRecursiveComparison()
.ignoringFields("id", "name", "workAddress", "netSalary", "grossSalary")
.isEqualTo(employee2);
}

@Test
public void givenEmployeesWithDifferentSalaries_whenComparingIgnoringFieldsMatchingRegex_thenEmployeesAreEqual() {

Employee employee1 = new Employee();
employee1.id = 1L;
employee1.name = "Jane Smith";
employee1.department = "Marketing";
employee1.homeAddress = "123 Home St";
employee1.workAddress = "456 Work Ave";
employee1.dateOfBirth = LocalDate.of(1990, 1, 1);
employee1.grossSalary = 95000.0;
employee1.netSalary = 70000.0;
public void givenEmployeesWithDifferentSalaries_whenComparingIgnoringFieldsMatchingRegexes_thenEmployeesAreEqual() {

Employee employee2 = new Employee();
employee2.id = 2L;
employee2.name = "Jane Smith";
employee2.department = "Marketing";
employee2.homeAddress = "789 Home St";
employee2.workAddress = "101 Work Ave";
employee2.dateOfBirth = LocalDate.of(1990, 1, 1);
employee2.grossSalary = 98000.0;
employee2.netSalary = 72000.0;
Employee employee1 = new Employee(1L, "Jane Smith", "Marketing", "456 Work Ave", 7000.0, 9500.0);
Employee employee2 = new Employee(2L, "Steve Jobs", "Marketing", "456 Work Ave", 7200.0, 9800.0);

Assertions.assertThat(employee1)
.usingRecursiveComparison()
.ignoringFields("id")
.ignoringFieldsMatchingRegexes(".*Address", ".*Salary")
.isEqualTo(employee2);
.usingRecursiveComparison()
.ignoringFieldsMatchingRegexes(".*id", ".*name", ".*Salary")
.isEqualTo(employee2);
}

@Test
public void givenEmployeesWithNullExpectedFields_whenComparingIgnoringExpectedNullFields_thenEmployeesAreEqual() {
// Given
Employee expectedEmployee = new Employee();
expectedEmployee.id = null;
expectedEmployee.name = "Alice Johnson";
expectedEmployee.department = null;
expectedEmployee.homeAddress = null;
expectedEmployee.workAddress = null;
expectedEmployee.dateOfBirth = LocalDate.of(1985, 5, 15);
expectedEmployee.grossSalary = null;
expectedEmployee.netSalary = null;

Employee actualEmployee = new Employee();
actualEmployee.id = 3L;
actualEmployee.name = "Alice Johnson";
actualEmployee.department = "HR";
actualEmployee.homeAddress = "789 Home St";
actualEmployee.workAddress = "123 Work Ave";
actualEmployee.dateOfBirth = LocalDate.of(1985, 5, 15);
actualEmployee.grossSalary = 90000.0;
actualEmployee.netSalary = 65000.0;
Employee expectedEmployee = new Employee(null, "Alice Johnson", null, null, null, null);
Employee actualEmployee = new Employee(3L, "Alice Johnson", "HR", "123 Work Ave", 6500.0, 9000.0);

Assertions.assertThat(actualEmployee)
.usingRecursiveComparison()
.ignoringExpectedNullFields()
.isEqualTo(expectedEmployee);
.usingRecursiveComparison()
.ignoringExpectedNullFields()
.isEqualTo(expectedEmployee);
}

@Test
public void givenEmployees_whenComparingIgnoringFields_thenContainsEqualElements() {
Employee actual1 = new Employee(1L, "John Doe", "Engineering", "456 Work Ave", 7500.0, 10500.0);
Employee actual2 = new Employee(2L, "John Doe", "Engineering", "101 Work Ave", 8000.0, 12000.0);
Employee expected1 = new Employee(3L, "John Doe", "Engineering", "Nil", 0.0, 0.0);
Employee expected2 = new Employee(4L, "John Doe", "Engineering", "Nil", 0.0, 0.0);

List<Employee> actual = List.of(actual1, actual2);
List<Employee> expected = List.of(expected1, expected2);

Assertions.assertThat(actual)
.usingRecursiveFieldByFieldElementComparatorIgnoringFields("id", "workAddress", "netSalary", "grossSalary")
.containsExactlyInAnyOrderElementsOf(expected);
}
}