Trying to work with assertj-core recursive comparison APIs for deep comparison between the objects as follows. But it appears that it doesn't work when we pass collections of custom objects for deep comparison when they are large in number for example even for 50 or 100, the comparison takes forever.
Let me share you the code chunks to look at.
- assertj core version: 3.24.2
- java version: 11
- test framework version: junit 4.13.1
- os (if relevant): Windows 10 64 bit
Minimal/simplified code reproducing the bug
Assignment class
class Assignment
{
String assignmentName;
String assignmentDescription;
Assignment(String assignmentName, String assignmentDescription)
{
this.assignmentName = assignmentName;
this.assignmentDescription = assignmentDescription;
}
}
Owner class
class Owner
{
String id;
String name;
ArrayList<Assignment> assignments;
Owner(String id,String name, ArrayList<Assignment> assignments)
{
this.id = id;
this.name = name;
this.assignments = assignments;
}
}
Code to generate a single Assignment object
public static Assignment GenerateAssignment()
{
return new Assignment("111", "Assignment 1");
}
Code to generate different assignment object
public static Assignment GenerateDifferentAssignment()
{
return new Assignment("222", "Assignment 2");
}
Code to generate list of Assignment objects
public static ArrayList<Assignment> GenerateListOfAssignments(Integer size)
{
ArrayList<Assignment> lst = new ArrayList<Assignment>();
for(Integer i=0 ; i<size; i++)
{
lst.add(GenerateAssignment());
}
return lst;
}
Code to generate different assignment objects
public static ArrayList<Assignment> GenerateDifferentListOfAssignments(Integer size)
{
ArrayList<Assignment> lst = new ArrayList<Assignment>();
for(Integer i=0 ; i<size; i++)
{
lst.add(GenerateDifferentAssignment());
}
return lst;
}
Code to generate an Owner object
public static Owner GenerateOwner(Integer numberOfAssignments)
{
ArrayList<Assignment> lstAssignments = GenerateListOfAssignments(numberOfAssignments);
return new Owner("owner1", "TestOwner 1", lstAssignments);
}
Code to generate a different Owner object
public static Owner GenerateDifferentOwner(Integer numberOfAssignments)
{
ArrayList<Assignment> lstAssignments = GenerateDifferentListOfAssignments(numberOfAssignments);
return new Owner("owner2", "TestOwner 2", lstAssignments);
}
Code to generate list of Owners
public static ArrayList<Owner> GenerateListOfOwners(Integer noOfOwners, noOfAssignments)
{
ArrayList<Owner> lstOwner = new ArrayList<Owner>();
for(Integer i=0 ; i<noOfOwners; i++)
{
lst.add(GenerateOwner(noOfAssignments));
}
return lstOwner;
}
Code to generate a different list of Owners
public static ArrayList<Owner> GenerateDifferentListOwners(Integer noOfOwners, noOfAssignments)
{
ArrayList<Owner> lstOwner = new ArrayList<Owner>();
for(Integer i=0 ; i<noOfOwners; i++)
{
lst.add(GenerateDifferentOwner(noOfAssignments));
}
return lstOwner;
}
JUnit test
@Test
public static void DeepCompareCollections()
{
//100 owners with 50 assignments each
ArrayList<Owner> actual = GenerateListOfOwners(100, 50);
//100 different owners with 50 different assignments each
ArrayList<Owner> expected = GeneratedDifferentListOfOwners(100, 50);
//Recursive comparison logic
var config = new RecursiveComparisonConfiguration();
config.ignoreCollectionOrder(true);
config.ignoreCollectionOrderInFields();
List<ComparisonDifference> difference = new RecursiveComparisonDifferenceCalculator().determineDifference(actual, expected,
config);
}
Once you debug the test : DeepCompareCollections test as shown above , you will observe that the recursive comparison takes a lot of time to determine the difference between the two collections. We can adjust the number of owners and their corresponding assignments to observe the behavior further.
Trying to work with assertj-core recursive comparison APIs for deep comparison between the objects as follows. But it appears that it doesn't work when we pass collections of custom objects for deep comparison when they are large in number for example even for 50 or 100, the comparison takes forever.
Let me share you the code chunks to look at.
Minimal/simplified code reproducing the bug
Assignment class
Owner class
Code to generate a single Assignment object
Code to generate different assignment object
Code to generate list of Assignment objects
Code to generate different assignment objects
Code to generate an Owner object
Code to generate a different Owner object
Code to generate list of Owners
Code to generate a different list of Owners
JUnit test
Once you debug the test : DeepCompareCollections test as shown above , you will observe that the recursive comparison takes a lot of time to determine the difference between the two collections. We can adjust the number of owners and their corresponding assignments to observe the behavior further.