0

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?

shavindip
  • 512
  • 3
  • 23
  • 1
    don't compare the ArrayLists, compare the contents of the lists. The lists themselves are different objects, for ArrayList, there is no overriden form of equals, so that equals will never return true – Stultuske Apr 16 '20 at 09:05
  • 1
    Does `Person` type has equals contract? – CodeScale Apr 16 '20 at 09:08
  • 1
    Arraylist equals contract ->`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.` – CodeScale Apr 16 '20 at 09:11
  • 1
    @Stultuske the `assertEquals` calls simply a null-safe `expected.equals(actual)`, and `ArrayList` has implemented equals, so I don't think the `Lists` are the problem, but the `Person` itself – Lino Apr 16 '20 at 09:11
  • @CodeScale The object Person has only String name and int age – shavindip Apr 16 '20 at 09:14
  • 1
    @shavindip have [you implemented `equals()` correctly](https://stackoverflow.com/questions/8180430/how-to-override-equals-method-in-java) ? – Lino Apr 16 '20 at 09:16
  • 1
    Please look his supertype...`AbstractList` equals is just inherited – CodeScale Apr 16 '20 at 09:26
  • @CodeScale missed that. thanks – Stultuske Apr 16 '20 at 09:27
  • @Stultuske It does in my java-jdk11.0.2_9 -> [ideone link of the snippet](https://ideone.com/Up0PNN) – Lino Apr 16 '20 at 09:29
  • 1
    @Lino I was looking in another version, where it doesn't, but CodeScale was right, it inherited an implementation from AbstractList I overlooked – Stultuske Apr 16 '20 at 09:33

1 Answers1

4

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.

CodeScale
  • 2,301
  • 1
  • 9
  • 16