Takkun
Takkun

Reputation: 6381

Junit isn't catching file not found exception

@Test(expected=FileNotFoundException.class)
public void downloadExcelTest() {
    stub(mockRoomService.getRoomByCode("ICU5")).toReturn(ICU5);
    stub(mockRS.makeExcel(anyInt(), any(Room.class))).toReturn("FILE_DOES_NOT_EXIST.xls");
    rc.downloadExcel(mockHTTP, 1, "ICU5", mockM);
}

So I'm trying to make a junit test to check for an exception, but it doesn't seem to be catching the exception. When I run the test, it says the expected exception did not occur, yet the following error is in the console.

java.io.FileNotFoundException: FILE_DOES_NOT_EXIST.xls (The system cannot find the file specified)

Is my syntax wrong or is it something else?

Upvotes: 0

Views: 2110

Answers (2)

worked
worked

Reputation: 5880

public void downloadExcel() throws IOException {
    try{
        ...
    }
    catch (NullPointerException e) { 
        throw new NullPointerException("Null exception"); 
    }
}

Upvotes: 0

fgb
fgb

Reputation: 18559

Junit won't detect the exception if it is caught within the method. You need to either throw the exception from downloadExcel, or find another way to notify the calling method of the error.

Upvotes: 2

Related Questions