2014-10-09 82 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

相关问题