Re: Need help designing some JUnit tests
On Fri, 21 May 2010, Patricia Shanahan wrote:
Rhino wrote:
Am I right in assuming that it is reasonable to bypass the sequence
testing in the case of my sorted list of Locales, given that I am using
a TreeMap which already guarantees the order? I assume you are just
including these tests to cover situations where I am generating a
sorted list out of thin air (without the benefit of the TreeXXX
Collection classes) so that I can be confident my code worked
correctly?
I would test it.
Suppose at some future time a programmer changes the Map implementation
e.g. from TreeMap to HashMap. That might be done in an attempt to
improve performance, or because of a future decision to standardize on
HashMap unless there is a specific reason to use a different
implementation. The future programmer could be yourself after you have
had time to forget why you used TreeMap.
Perhaps the method should be defined to return a SortedMap. If the test
says:
SortedMap<String, Locale> locales = LocaleHelper.getLocales(); // can't remember the actual name, sorry
Any attempt to return a non-Sorted Map, including redefining the method to
return a plain Map will be stopped in its tracks by the compiler.
Of course, it doesn't stop someone returning a TreeMap with a custom
comparator that does something bananas. Java's type system has its limits.
tom
--
If goods don't cross borders, troops will. -- Fr