jeudi 28 janvier 2016

How to test ParseException that is thrown by SimpelDateFormat? [duplicate]

This question already has an answer here:

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

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

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

main

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);
      }
}

results in java.text.ParseException: Unparseable date: "haha", haha.

Although a ParseException is thrown the test fails. Why?

Aucun commentaire:

Enregistrer un commentaire