I have a list of number which I have convert to int array list and sort that number. problem is which assert Lists differ at element [0]: 5 != 5 expected [5] but found [5] . I don't understand where the problem is in assert.
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);
OUTPUT
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
Assert fail
java.lang.AssertionError: Lists differ at element [0]:
5 != 5 expected [5] but found [5]
How can I pass this assert as expected and found value is same.
obtainedList
is a List<String>
, result
is a List<Integer>
they are never equals.
You have to use the same type on both lists to check for equality.
The method equals
of java.util.List
does the following:
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.
In your case the String "5"
doesn't equals the Integer 5
, so the test fails.