Mock fileAttributes
Есть функция, которая возвращает дату создания файла.
public static LocalDate getCreatedDateForLog(final File file) {
try {
BasicFileAttributes fileAttributes = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
return fileAttributes.creationTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}
catch (IOException e) {
LOG.warn("Can't read atributes of file: " + file.getName(), e);
return LocalDate.now();
}
}
Необходимо написать для нее unit-тесты:
@UnitTest
@RunWith(MockitoJUnitRunner.class) public class LogUtilsTest {
@Mock
private File file;
@Mock
BasicFileAttributes attributes;
private void initOldLog() {
Instant oldTimeLog = ZonedDateTime.now(ZoneId.systemDefault()).minusDays(5).toInstant();
when(attributes.creationTime()).thenReturn(FileTime.from(oldTimeLog));
when(file.toPath()).thenReturn(Paths.get("console-11111111.log"));
}
@Test
public void shouldReturnDate()
{
initOldLog();
assertEquals(LocalDate.now().minusDays(5),LogUtils.getCreatedDateForLog(file));
}
}
Но время создания файла не изменяется.