I am using Java 8 to write an application that interacts with the file-system. To make testing easier, I am injecting a FileSystem
into my methods, which can be swapped-out for testing. I am currently using jimfs
in my unit-tests and DefaultFileSystem.getFileSystem()
in production.
I would like my code to be platform independent and portable between unit-tests and production, however, I think that the classes in the standard library may prevent this.
For example, java.io.File
contains this:
private static final FileSystem fs = DefaultFileSystem.getFileSystem();
It seems that File
will always refer to real files, which is a problem for my unit-tests.
Path
, on the other hand, is just an interface, which should make things easy, except it has a method to create a File
!
File toFile();
- Which Java classes are tied to the real file-system?
- What are my options for writing
Filesystem
portable code?