How to force an IOException in jimfs

520 Views Asked by At

I have file related code to test where I would like to test my error handling for an existing file that I cannot read.

class SomeClass {
    //...
    public void load(Path path) {
       if (!Files.isRegularFile(path)) {
           return null;
       }
       //...
       try {
           // ...
       catch (IOException e) {
           // cleanup
       }
    }
}

I am using jimfs to isolate the tests from the real file system.

So how would I create a file on jimfs that is not readable?

I have already tried assigning posix permissions and a different user to the path through Files.setAttribute on the desired path, both of which seem to have been ignored when attempting to read or write to the path.

1

There are 1 best solutions below

1
On

Unfortunately there isn't currently any support for actual permission/access checking for files in Jimfs. It's something that I think would be good to have, but I haven't worked out the details: see this issue and this somewhat related issue.