antobbo
antobbo

Reputation: 285

Junit not catching FileNotFoundException

I runt into something strange. I have a method to read from a CSV file, line by line. The method takes the filePath and in my JUnit test, I'm testing this method with the wrong filePath expecting to get a FileNotFoundException. The thing is that JUnit5 doesn't throw that exception but in the eclipse console I can see that the JVM throws that exception, so I'm struggling to understand why

I've set up my test code to throw the exception but it doesn't get thrown. I tried to catch Exception but still no joy.

Here is the method and the test method

public void readData(String COMMA_DELIMITER, String READ_FILE_PATH) {
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(READ_FILE_PATH));
        String line = "";
        //Read to skip the header
        br.readLine();
        //Reading from the second line
        while ((line = br.readLine()) != null) 
        {

            String[] employeeDetails = line.split(COMMA_DELIMITER);
            populateModel(employeeDetails);
        }

        //Lets print the Employee List
        for(Employee e : empList)
        {
            System.out.println(e.getName() + "; " + e.getSurname() + "; " + e.getDateOfBirth() + "; " + e.getSex());
        }


    } 
    catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }

}

@Test
    void testWrongFilePath() {
        String READ_FILE_PATH_WRONG = System.getProperty("user.dir") + "/teest/XXXFile.csv";
        System.out.println(READ_FILE_PATH_WRONG);
        Assertions.assertThrows(FileNotFoundException.class, () -> {
            readData.readData(COMMA_DELIMITER, READ_FILE_PATH_WRONG);
        });     
    }

In the console, I get the FIleNotFOundException, but the output of the test says that

org.opentest4j.AssertionFailedError: Expected java.io.FileNotFoundException to be thrown, but nothing was thrown.

Upvotes: 0

Views: 2420

Answers (4)

Maciej Kowalski
Maciej Kowalski

Reputation: 26572

You cannot expect from your Assertion framework to catch an exception that is caught inside your SUT:

catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

You either have to :

  • Log then rethrow same / different exception and assert on that.

  • Make your method return Boolean as a success equivalent which you can then assert on.

Upvotes: 2

user3456014
user3456014

Reputation:

You're catching the FileNotFoundException within readData.

Try refactoring so that you don't have a try-catch, and have public void readData(String COMMA_DELIMITER, String READ_FILE_PATH) throws IOException { ...

(FileNotFoundException is a subclass of IOException.)

Upvotes: 1

Andy Turner
Andy Turner

Reputation: 140544

Your method doesn't throw FileNotFoundException: you catch it, print the stack trace, and carry on as if no exception occurred:

catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

JUnit isn't magic: it can't detect things that happen inside the method, other than by detecting side effects (values returned, uncaught exceptions, mutating state).

Upvotes: 0

davidxxx
davidxxx

Reputation: 131546

assertThrows(Class<T> expectedType, Executable executable)

doesn't assert that an exception is thrown at a time in your code (which is the true in your case). But that asserts that the statement invoked in the Executable lambda throws an exception (which is false in your case).

Since you caught the FileNotFoundException in the method under test, the exception is never propagates to the lambda return and JUnit can only emit an error because the expected exception was not encountered.

To assert such a thing, don't catch the exception by removing the catch statement and instead of declare throws FileNotFoundException in the declaration of the tested method :

public void readData(String COMMA_DELIMITER, String READ_FILE_PATH) throw FileNotFoundException {...}

Upvotes: 0

Related Questions