DeviceService { protected void wipeDirectory(File rootDirectory) { File[] listFiles = rootDirectory.listFiles(); if ( listFiles == null ) return; for (File file: listFiles) { if (file.isDirectory()) { wipeDirectory(file); } file.delete(); } } @Inject  DeviceService(RapidFtrApplication context); Boolean isBlacklisted(); void wipeData();  }
@Test public void shouldWipeExternalSdCardWhenRequested(){ File root = Mockito.mock(File.class); File picturesDirectory = Mockito.mock(File.class); File pictureFile = Mockito.mock(File.class); File rootFile = Mockito.mock(File.class); File[] listFiles = new File[2]; listFiles[0] = picturesDirectory; listFiles[1] = rootFile; File[] listFilesPictures = new File[1]; listFilesPictures[0] = pictureFile; doReturn(listFiles).when(root).listFiles(); doReturn(listFilesPictures).when(picturesDirectory).listFiles(); doReturn(true).when(picturesDirectory).isDirectory(); deviceService.wipeDirectory(root); verify(rootFile).delete(); verify(pictureFile).delete(); verify(picturesDirectory).delete(); }
@Test public void shouldNotIterateFileListIfDirectoryIsEmpty() { File root = Mockito.mock(File.class); File emptyDirectory = Mockito.mock(File.class); File[] filesList = new File[1]; filesList[0] = emptyDirectory; doReturn(filesList).when(root).listFiles(); doReturn(null).when(emptyDirectory).listFiles(); doReturn(true).when(emptyDirectory).isDirectory(); deviceService.wipeDirectory(root); verify(emptyDirectory).delete(); }