I've got a problem with mocking ZoneDateTime
and LocalTime
(Java 8) using PowerMock.
I use
@RunWith(PowerMockRunner.class)
@PrepareForTest({ZonedDateTime.class, LocalTime.class, Util.class})
for class and in test method
mockStatic(ZonedDateTime.class, LocalTime.class, Util.class);
PowerMockito
.doReturn(LocalTime.parse("23:56"))
.when(LocalTime.class, "now");
PowerMockito
.doReturn(ZonedDateTime.parse("2017-08-28T23:56:00Z", DateTimeFormatter.ISO_OFFSET_DATE_TIME))
.when(ZonedDateTime.class, "now", ZoneId.of(AMERICA_CHICAGO_ZONE));
PowerMockito
.doReturn("bombel")
.when(Util.class, "test");
Unfortunately, first two cases don't mock static methods. The third one, "test" which is written by me is mocked.
How mock ZonedDateTime
and LocalTime
?
I have to be sure that "now" points in particular moment in time. I don't think that wrapping ZoneDatetime.now() by another method is a good idea. Tests shouldn't impact on source code.