Force IOException during file reading

Posted by DixonD on Stack Overflow See other posts from Stack Overflow or by DixonD
Published on 2010-04-02T13:03:59Z Indexed on 2010/04/02 13:13 UTC
Read the original article Hit count: 201

Filed under:

I have the piece of code that reads data from file. I want to force IOException in this code for testing purpose (I want to check if code throws correct custom exception in this case).

Is there a some way to create a file which is protected from being read, for example? Maybe dealing with some security checks can help?

Please, note that passing name to not-existent file cannot help, because FileNotFoundException has separate catch clause.

Here peace of code for better undestanding of question:

    BufferedReader reader = null;
    try {

        reader = new BufferedReader(new FileReader(csvFile));

        String rawLine;
        while ((rawLine = reader.readLine()) != null) {
            // some work is done here
        }

    } catch (FileNotFoundException e) {
        throw new SomeCustomException();
    } catch (IOException e) {
        throw new SomeCustomException();
    } finally {
        // close the input stream
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }

© Stack Overflow or respective owner

Related posts about junit