Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Scanner;
- /**
- * Demo of how to test that a FileNotFoundException is being caught correctly.
- *
- */
- public class TestFNFException extends junit.framework.TestCase
- {
- /* Tries to open a file for reading.
- @returns false if can't find the file
- */
- private boolean read(String filename)
- {
- boolean result = true;
- try
- {
- Scanner ds = new Scanner(new File(filename));
- }
- catch (FileNotFoundException ex)
- {
- result = false;
- }
- return result;
- }
- public void testRead() throws IOException
- {
- File file = new File("xyz");
- file.createNewFile();
- assertTrue(read("xyz"));
- assertFalse(read("thisfiledoesntexist"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement