如何测试SimpelDateFormat抛出的ParseException?

时间:2016-01-28 18:50:58

标签: java testing exception-handling

@Rule
public final ExpectedException exception = ExpectedException.none();

@Test
public void doStuffThrowsParseException() {
    TestException foo = new TestException();

    exception.expect(ParseException.class);
    foo.doStuff("haha");
}

Date submissionT;
SimpleDateFormat tempDate = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");
public void doStuff(String time) {
    try {
        submissionT=tempDate.parse(time);
      }
      catch (Exception e) {     
        System.out.println(e.toString() + ", " + time);
      }
}

结果为java.text.ParseException: Unparseable date: "haha", haha

虽然抛出ParseException,但测试失败。为什么呢?

0 个答案:

没有答案