I'm using a third party library that works with java.io.File
instances. I'd like to use this library in my unit tests but I don't want it to create files on disk.
My first idea was to use JimFs to mock the file system but it does not support java.io.File.
My second idea was to mock File with some mocking framework and delegate calls to JimFs but don't know if that will work.
Is there some solution out there to use virtual java.io.File?
Jimfs is an in-memory file system that implements the Java NIO API and supports almost every feature of it. This is particularly useful, as it means we can emulate a virtual in-memory filesystem and interact with it using our existing java.
It is actually moving the file under src/test/resources. you can use PowerMock for mocking static methods or you may extract Files. move() to some method object and mock it with Mockito library. I would choose second one.
You could use a JUnit TemporaryFolder
rule to easily create temporary files that are deleted after the test method is complete.
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