junit中的ArrayList相等性虽然预期和实际相似,但不起作用

问题描述 投票:0回答:1

我正在使用以下测试方法来检查Arraylist是否按预期排序。

  @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);
  }

但是,尽管期望值和实际值相似,但与伙伴们一样发生了错误。

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>

我尝试了以下断言类型,但没有任何效果。

   assertTrue("check equality", Arrays.equals(expectedSortedResult.toArray(), actualSortedResult.toArray()));
   Assert.assertEquals(expectedSortedResult, actualSortedResult);
   assertEquals(expectedSortedResult, actualSortedResult);
   assertTrue(expectedSortedResult.equals(actualSortedResult));
   assertArrayEquals(expectedSortedResult, actualSortedResult);

我能知道我的方法有什么问题或应该做什么吗?

java testing arraylist junit qa
1个回答
3
投票

ArrayList等于合同说: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.

© www.soinside.com 2019 - 2024. All rights reserved.