user3746155
user3746155

Reputation: 45

"Unhandled exception type FileNotFoundException" in JUnit, despite throwing it in normal class?

Brace yourselves, Java beginner here. I wrote a method that reads a file to an integer array list.

 public static ArrayList<Integer> numArray(String numIn) throws FileNotFoundException{  

    try{
        FileReader file = new FileReader(numIn);
        Scanner sc = new Scanner(file);
        while(sc.hasNext())
        {
           statusCodes.add(sc.nextInt());
        }
        sc.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return statusCodes;
    }

It works fine when I run it in the main method of its own class, but when I try to run a JUnit Test Case for it, it gives me a FileNotFoundException.

static ArrayList<Integer> newCodes = numArray("src/statCodes.txt");

Does anybody know how to get rid of this exception?

Upvotes: 1

Views: 1642

Answers (6)

user10605919
user10605919

Reputation: 1

You may have forgotten to add another "throws FileNotFoundException" to the main method of your test case.

Upvotes: 0

a_river_in_canada
a_river_in_canada

Reputation: 299

Your catch statement catches ALL exceptions and doesn't re-throw them, so this method doesn't need the throw statement. Also if you have a method that throws an exception any time that method is called you must handle the exception that it throws.

Also are you running the JUnit tests inside the same package? I can't imagine that you wouldn't be but at least in eclipse it looks for the file in the local directory,which is the same for the JUnit and the main class.

Upvotes: 0

Ryan J
Ryan J

Reputation: 8323

Despite all the answers telling you that you don't need to throw an exception if you catch it, the reason you're likely getting the FileNotFoundException when you run your tests using JUnit is because your test classes are likely in a different directory and your path to the file is not valid in that situation. Try examining the value of numIn relative to your test classes and see if using an absolute path or some other solution works for you.

Upvotes: 0

Vivin
Vivin

Reputation: 1367

Thats because it is not able to find your file from directory. A good way to do this YourTestClass.class.getResourceAsStream(filename), this will return an InputStream. Also as everyone said, either remove your catch or throws clause.

Upvotes: 0

Smutje
Smutje

Reputation: 18173

That's because your method declares a (wrong) throwing of FileNotFoundException, which is caught in itself.

Upvotes: 1

Aniket Thakur
Aniket Thakur

Reputation: 69035

There is no need of declaring throws FileNotFoundException as you are catching Exception which will catch all Exceptions including catched and untcached(Runtime) Exception.

You are getting FileNotFoundException because file is not found at the path you have specified. Maybe try using absolute path? Check what is the root folder of your project and provide path accordingly. Also it will depend where are you running your junit cases from.

Upvotes: 1

Related Questions