I'd like to mock both LocalDate.now() and LocalDateTime.now() in unitTest. However, it seems the first LocalDateTime mock somehow doesn't work correctly if both are present.
@Test
void someTest(){
// create a fixed current date for testing
LocalDateTime fixedCurrentDate = LocalDateTime.of(2023, 3, 1, 0 ,0, 0);
// initialize the mock of the static method
MockedStatic<LocalDateTime> mockLocalDateTime = mockStatic(LocalDateTime.class, Mockito.CALLS_REAL_METHODS);
MockedStatic<LocalDate> mockLocalDate = mockStatic(LocalDate.class, Mockito.CALLS_REAL_METHODS);
// define the behaviour of the mock
mockLocalDateTime.when(LocalDateTime::now).thenReturn(fixedCurrentDate);
mockLocalDate.when(LocalDate::now).thenReturn(fixedCurrentDate.toLocalDate());
}
Error:
LocalDateTime cannot be returned by ofEpochDay()
ofEpochDay() should return LocalDate
If I only mock either of them, it works. what went wrong here?