Are there any Fake File System frameworks for Java?

devoured elysium picture devoured elysium · Aug 7, 2011 · Viewed 30.6k times · Source

I am introducing tests in a project that makes heavy use of IO operations (the file system, in this case). The system constantly opens/closes files, checks if files exist, deletes them, etcetera.

It soon became obvious that regular mocking wouldn't be of much use, as that would make my tests hard to set up and reason about. On the other hand, having a fake file system would be awesome, and I think, pretty easy to set up.

It seems the ruby guys did it again, and there's exactly what I am asking for in ruby: http://ozmm.org/posts/fakefs.html.

Is there anything remotely similar for Java?

Answer

Michael Deardeuff picture Michael Deardeuff · Jun 29, 2012

Google has an open-source, in-memory implementation of Java 7's FileSystemProvider. The project is called jimfs.


If you use Java 6 or earlier, there is an alternative: I've used Apache Commons VFS before to great success. It seems to be much like the custom FileSystemProvider another answerer mentioned is in Java 7.

It comes pre-loaded with several file-system implementations: File, RAM, S/FTP, and Jar to name a few. I've also seen a plugin for S3.