sriram sriram - 1 year ago 98
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?


Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download