Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any Fake File System frameworks for Java? [closed]

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?

like image 488
devoured elysium Avatar asked Aug 07 '11 01:08

devoured elysium


People also ask

Should unit tests use files?

For unit test I would suggest that you include the test file in your project(EAR file or equivalent) then use a relative path in the unit tests i.e. "../testdata/testfile". As long as your project is correctly exported/imported than your unit test should work.

How do I read a JUnit file?

File class to read the /src/test/resources directory by calling the getAbsolutePath() method: String path = "src/test/resources"; File file = new File(path); String absolutePath = file. getAbsolutePath(); System. out.


1 Answers

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.

like image 80
Michael Deardeuff Avatar answered Oct 01 '22 22:10

Michael Deardeuff