Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to JUnit test ParseException
    primarykey
    data
    text
    <p>So, I am working on a project, and I have to test if my method catches a <code>ParseException</code> when it occures. Here is how the method that I need to test looks like:</p> <pre><code>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()); } </code></pre> <p>So I have to test when this method catches a <code>ParseException</code>.</p> <p>The method that throws a ParseException is <code>projectDateToCalendarDefaultMin</code> , here is the code of that method:</p> <pre><code>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); </code></pre> <p>The method that throws ParseException is called <code>projectDateStringToCalendar</code>. Here is how that one looks like:</p> <pre><code>private static Calendar projectDateStringToCalendar(final String dateValue, final boolean useMinDate, final boolean useMaxDate) throws ParseException { if (useMinDate &amp;&amp; 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; } </code></pre> <p>So this is where the <code>ParseException</code> is finally thrown, at the <code>parse()</code> method. <code>ParseException</code> is from <code>text.parse</code> package and the method parse looks like this:</p> <pre><code> 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; } </code></pre> <p>What I have already tried to do is to set <code>bs.getFromDate</code> to null but the test would always be red. I used <code>@Test(expected: ParseException.class)</code> annotation in my test, but I just can't make it go green. Perhaps <code>bs.getFromDate</code> is not the right value that is being parsed? </p> <p>Does anyone have any other idea how to make this test work? Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload