Consider a DateUtilTest
class as follows that uses PowerMockRunner
:
import com.reporting.utils.DateUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.time.LocalDate;
import java.util.Date;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
@PrepareForTest(DateUtil.class)
@RunWith(PowerMockRunner.class)
public class DateUtilTest {
@Test
public void getPreviousWorkingDayAsDate_whenMonday() {
//given
LocalDate date = LocalDate.of(2017, 10, 16);
LocalDate expected = LocalDate.of(2017, 10, 13);
mockStatic(LocalDate.class);
when(LocalDate.now()).thenReturn(date);
//when
Date previousWorkingDay = DateUtil.getPreviousWorkingDayAsDate();
//then
assertEquals(DateUtil.getDateFromLocalDate(expected), previousWorkingDay);
}
@Test
public void getPreviousWorkingDayAsDate2_whenMonday() {
//given
LocalDate date = LocalDate.of(2017, 10, 16);
mockStatic(LocalDate.class);
when(LocalDate.now()).thenReturn(date);
//when
Date previousWorkingDay = DateUtil.getPreviousWorkingDayAsDate();
LocalDate expected = LocalDate.of(2017, 10, 13);
//then
assertEquals(DateUtil.getDateFromLocalDate(expected), previousWorkingDay);
}
}
I want to understand why @Test
==> getPreviousWorkingDayAsDate2_whenMonday
fails when I move the expected
LocalDate
initialization to after the mocking of the LocalDate.class
?
Further, could this tests be improved?