mardi 3 mars 2020

How to mock file that is opened by another process?

Question

How could we mock a file which is being used by another process?

I'm trying to either

  • safely open a file via another process during a test

or

  • emulate a file being opened by another process.

I was successful in using Process to open the file using Notepad on Windows 10. Although it works, it is not a portable, since Notepad is exclusive to Windows.


Background

We have a method which recursively deletes all the files within a directory, as well as the root directory itself, using Files.delete.

DeleteResult delete(Path rootDirectoryPath) {
    // ...
}

The value returned depends on whether we encounter a FileSystemException (or other exceptions) or not. We want to create a test to handle how our program reacts to these exceptions.


The exception I mentioned requires the file to be opened during the test. Our tests are automated, so we need to either trigger another process to open the file from the test itself, or emulate/replicate a file being used by another process.

Since this issue occurs with all files, we are testing using a text (.txt) file.

Aucun commentaire:

Enregistrer un commentaire