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?
If using JUnit 4.7 and up, you can use the @TemporaryFolder
rule to transparently obtain a temporary folder which should automatically get cleared after each test.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With