### Question:
ExportTreeManager { void clean() { File root = fileUtilsWrapper.getCineLogRoot(); File saveRoot = fileUtilsWrapper.getFile(root.getAbsolutePath() + "/saves/" + subDir); File[] saveFiles = saveRoot.listFiles(); if(saveFiles.length > 10){ File oldestFile = null; for (File saveFile : saveFiles) { if(oldestFile == null || oldestFile.lastModified() > saveFile.lastModified()){ oldestFile = saveFile; } } if(oldestFile != null){ oldestFile.delete(); } } } ExportTreeManager(String subDir);   ExportTreeManager(FileUtilsWrapper fileUtilsWrapper, String subDir);   }### Answer:
@Test public void clean() { doReturn(externalRoot).when(fileUtilsWrapper).getCineLogRoot(); doReturn("/root/CineLog").when(externalRoot).getAbsolutePath(); File saveRoot = mock(File.class); doReturn(saveRoot).when(fileUtilsWrapper).getFile("/root/CineLog/saves/movie"); File fileToRemove = mockFile(5L); doReturn(new File[]{ fileToRemove, mockFile(10L), mockFile(24L), mockFile(321L), mockFile(4444444L), mockFile(45L), mockFile(456456L), mockFile(1231321L), mockFile(789453L), mockFile(7777L), mockFile(987987L) }).when(saveRoot).listFiles(); new ExportTreeManager(fileUtilsWrapper, "movie").clean(); verify(fileToRemove).delete(); }
@Test public void cleanNotEnoughFiles() { doReturn(externalRoot).when(fileUtilsWrapper).getCineLogRoot(); doReturn("/root/CineLog").when(externalRoot).getAbsolutePath(); File saveRoot = mock(File.class); doReturn(saveRoot).when(fileUtilsWrapper).getFile("/root/CineLog/saves/movie"); File saveFile1 = mockFile(5L); File saveFile2 = mockFile(5L); doReturn(new File[]{ saveFile1, saveFile2, }).when(saveRoot).listFiles(); new ExportTreeManager(fileUtilsWrapper, "movie").clean(); verify(saveFile1, never()).delete(); verify(saveFile2, never()).delete(); }
@Test public void cleanNoFiles() { doReturn(externalRoot).when(fileUtilsWrapper).getCineLogRoot(); doReturn("/root/CineLog").when(externalRoot).getAbsolutePath(); File saveRoot = mock(File.class); doReturn(saveRoot).when(fileUtilsWrapper).getFile("/root/CineLog/saves/movie"); doReturn(new File[]{}).when(saveRoot).listFiles(); new ExportTreeManager(fileUtilsWrapper, "movie").clean(); }