Question

So, I am working on a project, and I have to test if my method catches a ParseException when it occures. Here is how the method that I need to test looks like:

public void convertToModel(final BuildStone bs, final TextBuildStone model) {
    try {
        model.setBeginDateRange(ProjectDateConverter.projectDateToCalendarDefaultMin(bs.getFromDate(), true));
    } catch (final ParseException e) {
        SystemContext.getLogger().warning(
                this,
                "treatGeneralData",
                "Date not converted: {0}, BeginDate set at MinDate",
                bs.getFromDate());
        model.setBeginDateRange(CalendarConstants.getMinDate());
    }

So I have to test when this method catches a ParseException.

The method that throws a ParseException is projectDateToCalendarDefaultMin , here is the code of that method:

public static Calendar projectDateToCalendarDefaultMin(final BigDecimal dateValue, final boolean useMinDate) throws ParseException {
    return dateValue == null ? ProjectDateConverter.projectDateStringToCalendar(null, useMinDate, false) : ProjectDateConverter
            .projectDateStringToCalendar(dateValue.toString(), useMinDate, false);

The method that throws ParseException is called projectDateStringToCalendar. Here is how that one looks like:

private static Calendar projectDateStringToCalendar(final String dateValue, final boolean useMinDate, final boolean useMaxDate)
        throws ParseException {
    if (useMinDate && useMaxDate) {
        throw new IllegalArgumentException("useMinDate and useMaxDate may not be set as true ");
    }
    if (StringUtils.isEmpty(dateValue)) {
        if (useMinDate) {
            return CalendarConstants.getMinDate();
        } else if (useMaxDate) {
            return CalendarConstants.getMaxDate();
        } else {
            return null;
        }
    }
...
final GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(new SimpleDateFormat("yyyyMMdd").parse(dateValue));

    return gc;
}

So this is where the ParseException is finally thrown, at the parse() method. ParseException is from text.parse package and the method parse looks like this:

 public Date parse(String source) throws ParseException
{
    ParsePosition pos = new ParsePosition(0);
    Date result = parse(source, pos);
    if (pos.index == 0)
        throw new ParseException("Unparseable date: \"" + source + "\"" ,
            pos.errorIndex);
    return result;
}

What I have already tried to do is to set bs.getFromDate to null but the test would always be red. I used @Test(expected: ParseException.class) annotation in my test, but I just can't make it go green. Perhaps bs.getFromDate is not the right value that is being parsed?

Does anyone have any other idea how to make this test work? Thanks in advance!

Was it helpful?

Solution

OK so the simplest way is to:

  • Call you method with arguments that will throw the ParseException
  • If no exception is thrown to the test it means it was caught in the method

Looking into your code there is another way you can do:
I see you set begin date range on the model if the exception has been caught: model.setBeginDateRange(CalendarConstants.getMinDate());

So in your test you can check if:
model.getBeginDateRange() equals CalendarConstants.getMinDate()
(You may need to get the actual version of the model before that check depending on how your code works)

Hope that helps.

OTHER TIPS

The JUnit @Test(expected) annotation requires that you be precise about which exception will be thrown.

If you assume ParseException, but NullPointerException is thrown, the test will show as failed.

Try @Test(expected = Exception.class) and see if that handles all of them.

Use a mock framework (e.g Mockito, EasyMock) to mock the call to the projectDateToCalendarDefaultMin method and throw a ParseException.

Your post condition assertion would be that the beginDateRange property of model has been set to CalendarConstants.getMinDate().

I think that another exception is thrown and you are not catching it. Namely, when you call this method:

projectDateToCalendarDefaultMin(final BigDecimal dateValue, final boolean useMinDate) throws ParseException {

with a null value for dateValue you get NullPointerExcpetion and that's why your test is red.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top