2014-10-09 91 views
-2

我需要將expected = ParseException.class添加到Junit方法,但即使將它添加到方法後,throwing行顯示「未處理的異常類型ParseException」消息,但我無法運行測試。爲什麼我不能在JUnit中定義期望的ParseException?

單位測試

import java.text.ParseException; 

@Test (expected=ParseException.class) 
public void testConvertDate() { 
    String date = "Tue 3434 20 23:33:44 EST 2014"; 
    MyDate mydate = new MyDate(); 
    Date result = instance.convertDate(date); //this line shows the message 
} 

方法

import java.text.ParseException; 
public Date convertDate(String d) throws ParseException { 
    .... 
} 
+0

只是在黑暗中拍攝。確保你的方法拋出的「ParseException」和你的測試期望的「ParseException」是一樣的 – Susie 2014-10-09 22:25:49

+0

@Susie它們是一樣的。 – Jack 2014-10-09 22:46:20

回答

2

由於異常是一個checked exception,需要聲明的或封閉在try/catch塊。在方法級別聲明它以允許它由JUnit進行管理。

@Test (expected=ParseException.class) 
public void testConvertDate() throws ParseException { 
0

你「convertDate(日期d)」的方法必須拋出ParseException的(確保它不抓住它...) 然後,它會傳播到你的測試方法女巫期待它。

+0

該方法正在拋出它。 – Jack 2014-10-09 22:10:26

相關問題