I need a test method that is inside a test class

So, I have a sample method that I give it a day and it returns me the first and last day of the week on which 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 need to write a JUnit test (and I did):

@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, since I used the code in dateToLocal () several times, I decided to do it this way:

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's not my question. I was wondering if it is correct to do this (method in this JUnit test), I need to make a test for this method, I need to move it to another class (outside of tests)? I know this is a strange question, I have already searched on google without success. (I may not be able to ask Google correctly). Thank:)

+3


source to share


5 answers


There is no absolute way to do something and it all depends on the project.

This section is open to all subjectivity.

IMO if you use a block of code multiple times in the same test class, that block must be retrieved by a local method.



If you use it in multiple test methods, you should learn inheritance or create a small use class in test suites.

If this method is also needed in your main java code, then you should write unit tests for it. The purpose of the tests is to test run-time code . JUnit code runs before execution begins.

+3


source


You don't need to test the testing method (as others have pointed out). However, I would recommend storing expected results as LocalDate instances rather than creating them on the fly from strings. This way, you can avoid possible errors in your conversion code.



+2


source


No, it's not a good idea to write "test for test". I have worked in many companies and have not done it anywhere. It is enough that you have written.

+1


source


It is perfectly acceptable to have helper methods in test classes. In fact, when testing things of any great complexity, it's hard not to. If the test helper method is in a utility class used in multiple tests across multiple projects (and thus being in the main jar of another project), then I generally don't find it useful to write tests for the test helper methods (otherwise it would be almost impossible to have code reuse and fully tested classes).

+1


source


I would say the rules for this are similar for test code and production code:

  • If you use the code template more than once in a class, extract it into a method
  • If you are using it in more than one class in one package, move the method to a utility class with package level visibility
  • If you use it in multiple packages, move the utility class to a dedicated utility package, make it public.
  • If you use code in more than one project, move it to a dedicated library
  • If you find that your entire organization needs this feature again, move it to the global library
  • If you find the whole world needs it, open source

Basically: make the code visible as it should be, but no more

And yes, if you extract your code into a utility class (2nd grade or lower) I would expect a dedicated test suite for the code

+1


source







All Articles