I am trying to write a unit test to test the sorting of two lists. What I have is I am calling the custom sorting comparator and then comparing the original list with the sorted list. Then from that I am using assertEquals to test whether the sorted list and original list match.
Let's say I have a simple model... patients.
Patient has two fields... name and age.
List<Patient> unOrdered = new ArrayList<Patient>();
List<Patient> ordered = new ArrayList<Patient>();
I fill these two up with the same three patients and order the ordered one properly.
ordered.add(new Patient("Chris Bacon", "45"));
ordered.add(new Patient("Charles Steak", "82"));
ordered.add(new Patient("Matt Pork", "32"));
Then I fill up the ordered one and order by age ascending.
unOrdered.add(new Patient("Matt Pork", "32"));
unOrdered.add(new Patient("Chris Bacon", "45"));
unOrdered.add(new Patient("Charles Steak", "82"));
So then, in my unit test, I write a Collections.sort with a custom comparator to order the unOrdered list by age ascending. I print that list out to the console during the test (for me) and then do...
assertEquals(ordered, unOrdered);
The console prints these lists out in identical order but the assertEquals returns false. I have even tried creating two completely identical lists in identical orders and trying assertEquals and it still returns false.
I am no Java expert but from what I have read online and the documentation assertEquals is not only checking for equality in the objects in the list but also the order of the objects. So... why is it always returning false? Is it that assertEquals cannot handle more complex objects or am I doing something wrong?
Aucun commentaire:
Enregistrer un commentaire