Your comment to @Nikolay's answer shows an important detail of your question that was hidden until now.
So, you want to test a method which uses a HashMap
structure for refering to some added objects and you want to test, if this method delivers some ordering in this structure? First added object shall remain at a "first position", last added object at a "last position"?
As the other answers already show, there is no way without refactoring that method. HashMap
doesn't deliver any meaningful ordering at all and if that method should deliver some ordering, it is simply broken - implementation is faulty.
Of course, you can write a unit test using the algorithm provided by @Sander. This test will fail most of the time. And this again shows the fact, that the tested method has to be refactured like @Nikolay showed in his answer, for instance.