Is this possible? I want a file to exist but when I try to read it, I want to have that read (Files.readAllBytes(filePath)
) to throw an IOException. This is so that I can unit test some functionality, so it just needs to happen through a JUnit test. I thought I could make it not readable, but that doesn't seem to work:
/**
* Should throw a IOException
*/
@Test
public void myTest() throws Exception {
File inputFile = createTempFile("test");
String args[] = {"-l", "-i", "--", "One", "Two", "--", inputFile.getPath()};
Runtime.getRuntime().exec("chmod 777 " + inputFile.getPath());
File file = new File(inputFile.getPath());
file.setExecutable(false);
Main.main(args);
assertEquals("Error", errStream.toString().trim());
}
Edit: I have also tried the solutions in this SO post, and they do not work either.
final RandomAccessFile raFile = new RandomAccessFile(inputFile.getPath(), "rw");
raFile.getChannel().lock();
String args[] = {"-l", "-i", "--", "One", "Two", "--", inputFile.getPath()};
Main.main(args);
When I run it in the debugger, I expect to see it hit an IOException on the read, but it reads the file correctly.
Here is how I am creating the temp files (in createTempFile)
File tmpfile = temporaryFolder.newFile();
tmpfile.deleteOnExit();
return tmpfile;