Are there fake file system frameworks for Java?

I introduced tests into a project that heavily uses I / O (in this case, the file system). The system constantly opens / closes files, checks if files exist, delete them, etc.

It soon became apparent that routine bullying would not make much sense, as this would complicate my tests and explain. On the other hand, having a fake file system would be awesome, and I think it's pretty easy to set up.

It seems the rubies guys did it again, and exactly what I ask at ruby: http://ozmm.org/posts/fakefs.html .

Is there anything remotely similar to Java?

+79
java unit-testing testing mocking
Aug 07 2018-11-11T00:
source share
12 answers

Google has an open source implementation of Java 7 FileSystemProvider in RAM. The project is called jimfs .




If you are using Java 6 or earlier, there is an alternative: I used Apache Commons VFS with great success before. It looks like this is very similar to the custom FileSystemProvider mentioned by another responder in Java 7.

It is preloaded with several file system implementations: File, RAM, S / FTP and Jar. I also saw a plugin for S3 .

+48
Jun 29 2018-12-12T00:
source share

In Java 6 and earlier, this is difficult because classes such as File and FileInputStream do not allow sending to different virtual file systems in the Java space.

Java 7 supports virtual file systems; see Developing a custom file system provider . I don’t know if this will allow you to do what you want to do, but it is a good place to look.




Fur. Being the fact that there really isn't any fake file system, I assume that I just implement the minimal implementation myself. I won’t win anything using FileSystemProvider

In fact, you win with FileSystemProvider:

  • You realize that (if it is released under an open source license) can be very useful for other people in your position and for other purposes.

  • You simplify yourself if you decide to switch to FileSystemProvider, which someone else can work with now.

+34
Aug 07 '11 at 3:18
source share

You can use org.junit.rules.TemporaryFolder from the JUnit package:

The TemporaryFolder rule allows you to create files and folders that are guaranteed to be deleted when the testing method is completed (regardless of whether it passes or does not work):

Example:

 final TemporaryFolder testFolder = new TemporaryFolder(); testFolder.create(); final Path filePath = testFolder.newFile("input.txt").toPath(); final Path dirPath = testFolder.newFolder("subfolder").toPath(); 

Alternatively exit the .toPath() :

 final File filePath = testFolder.newFile("input.txt"); 
+12
Jul 07 '14 at 11:51
source share

You can abstract the use of File using the intent of "writing data somewhere", changing your API to use OutputStream instead of File , then pass the FileOutputStream API in the production code, but pass it to ByteArrayOutputStream from your tests., ByteArrayOutputStream is a stream in memory, so it is very fast and you can just check its contents using its methods - it is perfect for testing. There is also a corresponding ByteArrayInputStream if you want to read the data.

File systems are generally pretty fast - if you hadn’t done a lot of file I / O in your tests, I wouldn’t worry.

Note that creating a File Java object does not create a file on disk, that is, the following code does not cause any changes to disk:

 File f = new File("somepath"); // doesn't create a file on disk 
+8
Aug 07 '11 at 2:30 a.m.
source share

Google's Jimfs is a memory NIO file system that is great for testing.

+6
Jan 26 '15 at 12:23
source share

A simple way would be to use your system method of providing a completely RAM-based file system - tempfs on Linux, and a RAM disk on Windows.

+4
Aug 07 '11 at 2:15
source share

MockFTPServer seems to have several Fake Fileystem implementations (Unix / Windows)

It looks like you can use these fake file system implementations completely separate from any FTP concepts. I am trying to do it now exactly as you described.

+4
Aug 31 2018-11-21T00:
source share

I'm not sure about specific frameworks, but the general approach from the OOP point of view is to write some abstract layers on top of any file access code (interfaces are plentiful!) And possibly a facade to facilitate the use of common operations. then you are simply mocking one layer below the code that you are testing now, and then essentially a fake file system (or at least the code that you are testing will not know otherwise).

If you learn to use the dependency framework to handle this for you, it will make it easier to switch components for a fake interface implementation. if you follow the control inversion patterns, passing any dependencies to the constructor of the class you are testing will also make testing easier.

 public interface IFileSystem { IFileHandle Load(string path); //etc } public class ClassBeingTested { public ClassBeingTested(IFileSystem fileSystem) { //assign to private field } public void DoSomethingWithFileSystem() { //utilise interface to file system here //which you could easily mock for testing purposes //by passing a fake implementation to the constructor } } 

I hope that my java is correct, I have not written java for a long time, but you hopefully get a drift. hope i don't underestimate the problem here and oversimplified!

Of course, all this assumes that you mean true unit testing, that is, testing the smallest possible units of code, and not the entire system. integration testing requires a different approach.

+2
Aug 07 2018-11-11T00:
source share

ShrinkWrap from Arquillian Project Supports NIO-Compatible FileSystem Inclusion

You can create an in-memory FileSystem by doing the following:

 FileSystem fs = ShrinkWrapFileSystems.newFileSystem(ShrinkWrap.create(GenericArchive.class)) 
+2
Sep 05 '13 at 21:27
source share

Two others in java memory file systems:

memoryfilesystem

ephemeralfs

Both implement the NIO.2 file system.

+1
Apr 6 '15 at 4:32
source share

I googled "Fake java FileSystem" and found this question. Unfortunately, that’s all I found. So I wrote this fake file system myself: https://github.com/dernasherbrezon/mockfs

I use it to simulate an IOException while reading / writing to files. An IOException can occur, for example, due to a "lack of disk space", which is almost impossible to simulate by other means.

0
Apr 20 '19 at 20:02
source share

A bit old, and this solution looks like Linux, but it looks good https://www.google.co.il/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=tmpfs%20on%20ubuntu

tmpfs is a memory-mapped folder (data disappears upon reboot). After installation, the data can be copied to it and work with memory.

-one
Aug 16 '16 at 8:51
source share



All Articles