Issue
I am using below test method to check whether the Arraylist is sorted as expected.
@Test
void compareFields()
{
Assignment14 assignment14 = new Assignment14();
ArrayList<Person> PeopleList = new ArrayList<Person>();
PeopleList.add(new Person("Alex",56));
PeopleList.add(new Person("Thomas",23));
PeopleList.add(new Person("John",10));
ArrayList<Person> actualSortedResult = assignment14.compareFields(PeopleList);
ArrayList<Person> expectedSortedResult = new ArrayList<Person>();
expectedSortedResult.add(new Person("Alex",56));
expectedSortedResult.add(new Person("John",10));
expectedSortedResult.add(new Person("Thomas",23));
Assert.assertEquals(expectedSortedResult, actualSortedResult);
}
But although the expected and the actual are similar an error is occuring as fellows.
java.lang.AssertionError: expected: java.util.ArrayList<[Person{name='Alex', age=56}, Person{name='John', age=10}, Person{name='Thomas', age=23}]> but was: java.util.ArrayList<[Person{name='Alex', age=56}, Person{name='John', age=10}, Person{name='Thomas', age=23}]>
Expected :java.util.ArrayList<[Person{name='Alex', age=56}, Person{name='John', age=10}, Person{name='Thomas', age=23}]>
Actual :java.util.ArrayList<[Person{name='Alex', age=56}, Person{name='John', age=10}, Person{name='Thomas', age=23}]>
<Click to see difference>
I have tried the below assertion types but nothing worked.
assertTrue("check equality", Arrays.equals(expectedSortedResult.toArray(), actualSortedResult.toArray()));
Assert.assertEquals(expectedSortedResult, actualSortedResult);
assertEquals(expectedSortedResult, actualSortedResult);
assertTrue(expectedSortedResult.equals(actualSortedResult));
assertArrayEquals(expectedSortedResult, actualSortedResult);
Can I know what is wrong with my method or what should be done?
Solution
ArrayList
equals contract said this :
Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal.
So looking at your error you have to check the equals contract of Person
type. If you don't override the equals for this type the references are compare and so your lists are not equals
So you have to override equals
method based on name and age fields to solve your problem.
Answered By - CodeScale
Answer Checked By - Mary Flores (JavaFixing Volunteer)