Im starting with mock in my unit tests. Wanted to upgrade method to using files. Mocks with some strings or numbers work, but now code has failed test. I have class with method:
public class FileService {
public List<File> findFilesUsingSuffix(File root, String suffix) {
List<File> list = new ArrayList<>();
if (root.isDirectory()) {
File[] files = root.listFiles();
if (files != null) {
for (File file : files) {
list.addAll(findFilesUsingSuffix(file, suffix));
}
}
} else if (root.getName().endsWith(suffix)) {
list.add(root);
}
return list;
}
}
and test:
public class FileServiceTest {
private FileService fileService;
private TemporaryFolder temporaryFolder;
@Before
public void setUp() throws Exception {
fileService = mock(FileService.class);
temporaryFolder = mock(TemporaryFolder.class);
File file1 = temporaryFolder.newFile("file.java");
File file2 = temporaryFolder.newFile("file.swf");
File file3 = temporaryFolder.newFile("file2.java");
File file4 = temporaryFolder.newFile("picture.jpg");
File file5 = temporaryFolder.newFile("photo.jpg");
File file6 = temporaryFolder.newFile("movie.mp4");
File file7 = temporaryFolder.newFile("music.mp3");
File file8 = temporaryFolder.newFile("someFile.java");
File file9 = temporaryFolder.newFile("someDoc.doc");
File file10 = temporaryFolder.newFile("excelThing.xls");
File file11 = temporaryFolder.newFolder("java");
}
@Test
public void findFilesUsingSuffixIsTrue() throws IOException {
//given
List<File> expectedList = List.of(
new File("file.java"), new File("file2.java"), new File("someFile.java")
);
when(fileService.findFilesUsingSuffix(ArgumentMatchers.any(File.class),any(String.class))).thenCallRealMethod();
//when
List<File> result = fileService.findFilesUsingSuffix(temporaryFolder.getRoot(), ".java");
//then
assertEquals(expectedList, result);
}
}
I'm getting empty list in result, but idk what i did incorrectly
Aucun commentaire:
Enregistrer un commentaire