Friday, 15 August 2014

java File.listFiles() order not guaranteed and causes JUnit tests to fail -



java File.listFiles() order not guaranteed and causes JUnit tests to fail -

i ran funny situation because java's file.listfiles() method returns list of files in directory without guarantees on order. 1 class reads info files in directory using listfiles() method , writes outputstream. corresponding junit test cases tries compare info written output stream expected bytearray. test passed on dev machine , production stack, failed on dev server because uses different os , different version of java.

in how file.listfiles in alphabetical order? solution forcefulness sorting of array returned file.listfiles(). not comfortable solution because doing work junit test can pass.

so wondering if there improve way. advice? thanks

i not see problem sorting list. not doing unit test: reducing randomness in scheme thought (it makes testing easier, , when problems occur, makes them easier reproduce). it's true doing work, unless reading thousands , thousands of files, sorting unlikely bottleneck (of course, profile regularly).

so in opinion, sorting best option.

if don't want / cannot this, other alternative create unit test smarter; example, don't compare whole bytearray in 1 go, check total size , result each file nowadays in array. or parse , sort array in test (though may complicated).

java junit

No comments:

Post a Comment