user10973120
user10973120

Reputation:

How to write Junit test for this "FileNotFoundException"

How do I write a Junit test for FileNotFoundException, do I need to do something in the test so that my "numbers.txt" file is not seen?

public void readList() {
        Scanner scanner = null;
        try {
            scanner = new Scanner(new File("numbers.txt"));

            while (scanner.hasNextInt()) {
                final int i = scanner.nextInt();
                ListOfNumbers.LOGGER.info("{}", i);


            }
        } catch (final FileNotFoundException e) {
            ListOfNumbers.LOGGER.info("{}","FileNotFoundException: " + e.getMessage());
        } finally {
            if (scanner != null) {
                ListOfNumbers.LOGGER.info("{}","Closing PrintReader");
                scanner.close();
            } else {
                ListOfNumbers.LOGGER.info("{}","PrintReader not open");
            }
        }

    }

Upvotes: 2

Views: 11035

Answers (2)

mate00
mate00

Reputation: 2925

Actually, what you are planning to do is testing JVM itself, to see if proper exception is thrown under certain conditions. Some argue, that it's not unit testing anymore, and you need to make assumption that what's on external, JMV's side simply works and doesn't need to be tested.

Your method readList() is highly untestable. You want to write a test for file existance, but you create a file object inside that method instead of injecting it. You want to see if exception is thrown, but you catch it inside that method.

Let's externalize that:

public void readList(File inputFile) throws FileNotFoundException {
  //... do your code logic here ...
}

You could then use JUnit's @Rule called ExpectedException in your unit test:

@RunWith(MockitoJUnitRunner.class)
public class ReaderTest {

  @Rule
  public ExpectedException exception = ExpectedException.none(); // has to be public

  private YourReader subject = new YourReader();

  @Test(expect = FileNotFoundException.class)
  public void shouldThrowFNFException() {
    // given
    File nonExistingFile = new File("blabla.txt");

    // when
    subject.readList(nonExistingFile);
  }

  // ... OR ...

  @Test
  public void shouldThrowFNFExceptionWithProperMessage() {
    // given
    File nonExistingFile = new File("blabla.txt");

    exception.expect(FileNotFoundException.class);
    exception.exceptionMessage("your message here");

    // when
    subject.readList(nonExistingFile);
  }
}

Upvotes: 3

Swapnil Patil
Swapnil Patil

Reputation: 613

You can expect for FileNotFoundException once your readList() wont find numbers.txt file. Also, you are handling the FileNotFoundException hence you need to throw it again into the catch block.

Try Like:

@Test(expected = java.io.FileNotFoundException.class)
public void testReadListForFileNotFoundException(){
// call your method readList
}

Throw it so that your test case will expect it.

catch (final FileNotFoundException e) {
   ListOfNumbers.LOGGER.info("{}","FileNotFoundException: " + e.getMessage());
   throw e;
}

Upvotes: 0

Related Questions