2017-02-13 3 views
0

Ich habe ein Stück Code mit try-catch-Block:Junit mit Ausnahme

public static void writeExcelToFile(String outFileName, HSSFWorkbook workBook) throws IOException{ 
    File file = null; 
    FileOutputStream fileOutputStream = null; 
    try { 
     file = getFileByFileName(outFileName); 
     File parent = file.getParentFile(); 
     Path filePath = parent.toPath(); 
     if (Files.notExists(filePath) && !parent.mkdirs()) { 
      throw new IOException("Couldn't create dir: " + parent); 
     } 
     fileOutputStream = new FileOutputStream(file); 
     workBook.write(fileOutputStream); 
    } catch (FileNotFoundException fileNotFoundException) { 
     LOGGER.error("File path is invalid, file not found ", fileNotFoundException); 
     throw fileNotFoundException; 
    } catch (IOException ioException) { 
     LOGGER.error("Exception occured while reading writing file ", ioException); 
     throw ioException; 
    } catch (Exception exception) { 
     LOGGER.error("Exception occured ", exception); 
     throw exception; 
    } finally { 
     if (fileOutputStream != null) { 
      fileOutputStream.close(); 
     } 
    } 
    file.setWritable(true); 
} 

ich folgende Junit für den Fang Block geschrieben haben:

//#1: FileNotFoundException 
@Test(expected = java.io.FileNotFoundException.class) 
public void testWriteExcelToFileException() throws IOException { 
    PowerMockito.mockStatic(KnewtonCIExcelWriter.class); 
    PowerMockito.doThrow(new java.io.FileNotFoundException()).when(KnewtonCIExcelWriter.class); 
    KnewtonCIExcelWriter.writeExcelToFile(anyString(), anyObject()); 
} 

//#2: IOException 
@Test(expected = IOException.class) 
public void testWriteExcelIOException() throws IOException { 
    PowerMockito.mockStatic(KnewtonCIExcelWriter.class); 
    PowerMockito.doThrow(new IOException()).when(KnewtonCIExcelWriter.class); 
    KnewtonCIExcelWriter.writeExcelToFile(anyString(), anyObject()); 
} 

//#3: Exception 
@Test(expected = Exception.class) 
public void testWriteExcelException() throws IOException { 
    PowerMockito.mockStatic(KnewtonCIExcelWriter.class); 
    PowerMockito.doThrow(new Exception()).when(KnewtonCIExcelWriter.class); 
    KnewtonCIExcelWriter.writeExcelToFile(anyString(), anyObject()); 
} 

jedoch nur die letzte, # 3 Junit vergeht. # 1 und # 2 gibt java.lang.AssertionError: Erwartete Ausnahme: java.io.FileNotFoundException und java.lang.AssertionError: Erwartete Ausnahme: java.io.IOEXception.

Question: 1) How to get the #1 and #2 JUnit passing? 2) Am I catching the correct exceptions?

+0

Bitte PowerMock Dokumentation lesen, 'PowerMockito.doThrow (new Exception()), wenn (KnewtonCIExcelWriter.class).' Dies fehlt ein Funktionsnamen –

+1

Hinweis: Ausnahmen nicht abfangen, protokollieren und erneut auslösen. Loggen Sie sie ein oder wiederholen Sie sie; mach beides nicht. –

Antwort

0

powermockito Syntax auf doThrow sollte, wenn sein

when(KnewtonCIExcelWriter.class, "your method", argument (you can user matcher)); 
Verwandte Themen