sriram sriram - 24 days ago 10
Java Question

test equality of two iterators?

How do i test for the equality of two iterators using JUnit?

Is there any built in method or is it just by comparing each and every element in that?

Thanks,
Sriram

Answer

There is no sane way to test equality of Iterators.

If going insane in this is an option, you may want to delve into the implementation of the specific iterator type you are testing, and use reflection to access private stuff and compare that (I'm sure, with enough analysis of source code, that you would find what needs to hold for two ListIterators to be equal, for example).

Or if it's your own iterator type, then export some variables that help you out and use them instead of reflection.

Iterating through and comparing elements is only a weak guarantee, as you could be iterating over a clone of your collection, meaning the iterators look the same, but aren't.

Just don't do this.