I am currently writing a unit test that is checking if a method is correctly sorting a list.
The sorting method is overrding a compare
method from the clas Comparator
and is sorting the list using Collections.sort()
.
It may not be a technical question, but I am looking for a way to use JUnit's assertions to check if the list is correctly sorted...
The list is sorted by an inner parameter of the type it is holding, let me call it id
. So when the list contains 3 items with ID's: 3,1,2 - it shall sort them as 1,2,3.
Long expected1 = listOfObjects.get(0).getId()
Long expected2 = listOfObjects.get(1).getId()
Long expected3 = listOfObjects.get(2).getId()
And then using asserions on those Long objects ain't looking clean nor clever. I am looking for an idea how to cleanly and cleverly test this case, but I'm out of ideas...