排序断言失败

Sorting order Assert fail

我有一个数字列表,我已将其转换为 int 数组列表并对该数字进行排序。 问题是哪个断言列表在元素 [0] 处不同:5 != 5 expected [5] but found [5] 。我不明白问题出在哪里。

ArrayList<String> obtainedList = new ArrayList<>();
List<WebElement> elementList = driver.findElements(By.xpath("//mat-table//mat-row/mat-cell[2]"));
for (WebElement we : elementList) {
    obtainedList.add(we.getText());
}
List<Integer> result = obtainedList.stream().map(Integer::valueOf).sorted() // sort the elements
        .collect(Collectors.toList());
Collections.sort(result);
//Collections.reverse(result);
Reporter.log(AddRule + obtainedList + result + " Cloumn is display in  Ascending order");
Add_Log.info(AddRule + obtainedList + result + " Cloumn is display in  Ascending order");
Assert.assertEquals(result, obtainedList);

输出

 No.[5, 7, 8, 10, 11, 12, 19, 22, 92, 96, 98, 99]
    [5, 7, 8, 10, 11, 12, 19, 22, 92, 96, 98, 99] 
 Cloumn is display in  Ascending order

断言失败

java.lang.AssertionError: Lists differ at element [0]: 
5 != 5 expected [5] but found [5]

如何按预期传递此断言并找到相同的值。

obtainedList 是一个 List<String>result 是一个 List<Integer> 他们永远不相等。

您必须在两个列表上使用相同的类型来检查是否相等。

java.util.List 的方法 equals 执行以下操作:

Compares the specified object with this list for equality. 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. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.

在您的情况下,字符串 "5" 不等于整数 5,因此测试失败。