vendredi 31 mars 2017

Do I need test method that is inside the test class

So I have a sample method that I give him a day and it returns me the first and last day of a week in which the day:

public static final DatePeriod thisWeek(LocalDate date) {

    TemporalField dayOfWeek = WeekFields.of(Locale.FRANCE).dayOfWeek();
    LocalDate mon = date.with(dayOfWeek, 1);
    LocalDate sun = date.with(dayOfWeek, 7);

    return new DatePeriod(mon, sun);
}

I have to write a JUnit test (and I did it):

@Test
public void testThisMonth_LocalDate() throws ParseException {
    System.out.println("thisMonth");
    for (String[] date : VALID_TEST_DATES) {
        LocalDate dateLocal = dateToLocal(date[0]);
        DatePeriod expResult = new DatePeriod(dateToLocal(date[5]), dateToLocal(date[6]));
        DatePeriod result = JavaTimeUtils.thisMonth(dateLocal);
        assertEquals(expResult.getLeft(), result.getLeft());
        assertEquals(expResult.getRight(), result.getRight());
    }
}

So, because I used code in dateToLocal() multiple times, I decided to make it in method like this:

public LocalDate dateToLocal(String dateString) throws ParseException {   // Calendar to LocalDate
    DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    Calendar cal = Calendar.getInstance();
    Date initDate = df.parse(dateString);
    cal.setTime(initDate);
    LocalDate dateLocal = LocalDate.of(cal.get(Calendar.YEAR), Month.of(cal.get(Calendar.MONTH)+1), cal.get(Calendar.DAY_OF_MONTH));

    return dateLocal;
}

And it works. But that is not my question. I was wondering is this the correct way to do things like this (method in this JUnit test), do I need to make Test for this method, do I have to move it in other class (outside the tests)? I know this is strange question, I've already searching in google, unsuccessfully. (maybe I can't ask google right). Thanks :)

Aucun commentaire:

Enregistrer un commentaire