I have a method like below ,
public static boolean isBetweenMonthDays(int startDayOfMonth,
int endDayOfMonth) {
LocalDate today = LocalDate.now();
if (today.getDayOfMonth() >= startDayOfMonth
&& today.getDayOfMonth() <= endDayOfMonth) {
return true;
}
return false;
}
This is a simple method in utility class but there are many other complicated methods too and result is dependent on today's date so my asserts will fail on certain dates.
What is preferred to unit test such methods?
UPDATE: are these below kind of unit tests considered bad practice since most are advising in injecting LocalDate
dependency & making class testable?
I am hesitant in doing that since its a static utility class and clients work only on integers?
@Test
public void testIsBetweenMonthDays_success_true() {
LocalDate today = LocalDate.now();
boolean result = ResponseMatchingUtil.isBetweenMonthDays(10, 25);
if (today.getDayOfMonth() >= 10 && today.getDayOfMonth() <= 25)
assertTrue(result);
else
assertFalse(result);
}
@Test
public void testIsBetweenMonthDays_success_false() {
LocalDate today = LocalDate.now();
boolean result = ResponseMatchingUtil.isBetweenMonthDays(4, 10);
if (today.getDayOfMonth() >= 4 && today.getDayOfMonth() <= 10)
assertTrue(result);
else
assertFalse(result);
}