I want to assert items based on their index in the list under test.
While doing some research I came across this solution, which works:
// This works:
assertThat(tasks).extracting(Task::getId, Task::getExamId).containsExactly(
tuple(1, EXAM_ID),
tuple(2, EXAM_ID),
tuple(3, EXAM_ID),
tuple(4, EXAM_ID),
tuple(5, EXAM_ID)
);
However I am looking for something more dynamic, FOR EXAMPLE (doesn't compile)
// I would rather use something like
BiFunction<Integer, Task, Boolean> indexedPredicate = (n, task) -> Objects.equals(task.getId(), n);
assertThat(tasks).allSatisfy(indexedPredicate);
I had a good look at all available methods but couldn't find anything. Any ideas?strong text
As we wanted to assert the relation between index and the element, how about converting the list to index-element map first?
Then we could call allSatisfy to assert the relationship.
Reference: How to convert List to Map with indexes using stream - Java 8?