Advertisement
jdalbey

FileNotFoundException Test Demo

Mar 16th, 2015
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.81 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3. /**
  4.  * Demo of how to test that a FileNotFoundException is being caught correctly.
  5.  *
  6.  */
  7. public class TestFNFException extends junit.framework.TestCase
  8. {
  9.     /* Tries to open a file for reading.
  10.        @returns false if can't find the file
  11.      */
  12.     private boolean read(String filename)
  13.     {
  14.         boolean result = true;
  15.         try
  16.         {
  17.             Scanner ds = new Scanner(new File(filename));
  18.         }
  19.         catch (FileNotFoundException ex)
  20.         {
  21.             result = false;
  22.         }
  23.         return result;
  24.     }
  25.  
  26.     public void testRead() throws IOException
  27.     {
  28.         File file = new File("xyz");
  29.         file.createNewFile();
  30.         assertTrue(read("xyz"));
  31.         assertFalse(read("thisfiledoesntexist"));
  32.     }
  33.  
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement