I have a class that does operations on file’s on a disk.
More exactly it traverses a directory, reads through all files with a given suffix
and does some operations on the data and then outputs them to a new file.
I’m a bit dubious as to how to design a unittest for this class.
I’m thinking having the setup method create a temporary directory and temporary files in /tmp/somefolder, but I’m suspecting this is a bad idea for a couple of reasons(Developers using windows, file permissions etc).
Another idea would be to mock the classes I’m using to write and read to the disk, by encapsulating the classes using an interface and then providing a mock object, but it seems to be a bit messy.
What would be the standard way of approaching such a problem?
Your strategy is the right one, IMO. Just make sure not to hardcode the temp directory. Use
System.getProperty("java.io.tmpdir")to get the path of the temp directory, and use a finally block in your test or a@Aftermethod to cleanup the created files and directories once your test is finished.