mardi 27 janvier 2015

Unit test that an exception has been caught and handled

I have written a method which uses BufferedReader and FileReader, and a result of this is that FileNotFoundException and IOException need to be handled.


Now I have utilised try and catch statements within my class to handle the exceptions so no issues there.


What I am having an issue with is writing a test which verifies that the exceptions have been caught.


I can't write a test as follows because my method already catches the exception so it is not propagated to be dealt with in the test:



@Test(expected = FileNotFoundException.class)
public void testFileNotFound() {
...
};


How could I test that the exception has been caught? Would I need to create a mock object of my class and verify that the exception has been caught using something like Mockitio? Or not create a mock and use Mockitio to spy on the actual object to verify if the exception has been caught?


Aucun commentaire:

Enregistrer un commentaire