如何测试junit中是否抛出并捕获异常

Raj*_*Raj 1 java junit

我需要测试以下代码:

try{
   while((testBean = csvReader.read(TestBean.class,headers,getCellProcessors()))!=null){
      System.out.println("no excpetion");
      i=5;
   }
}catch(SuperCsvException csvExc){
   System.out.println("superCSV excpetion");
   i=0;
}catch(Exception ex){
   System.out.println("excpetion");
   i=0;
}
Run Code Online (Sandbox Code Playgroud)

如何测试是否SuperCsvException被抛出和被捕获。

Sco*_*ion 5

JUnit 4 supports this
Run Code Online (Sandbox Code Playgroud)
@Test(expected=<YourExpectedException>.class)
public void testExceptionThrown() {
// call the method that throws exception
    }
Run Code Online (Sandbox Code Playgroud)