Reputation: 101
I'm having difficult time figuring out how to test a directory that I've made myself and imported in the Project Files.
Currently I have:
File file = new File(ExplorerView.class.getResource("/ExplorerViewTestFolder"));
Which I'm trying grab from here to test
ExplorerView explorer = new ExplorerView();
explorer.countFilesAndFolders("/ExplorerViewTestFolder");
Edit2: Changed to
public class ExplorerViewTests {
@Test
public void testCountFilesAndFolders() {
ExplorerView explorer = new ExplorerView();
explorer.countFilesAndFolders("/ExplorerViewTestFolder");
}
EDIT4:
public int countFilesAndFolders(File f) {
if (f.isFile()) {
return 1;
} else {
int total = 0;
for (File file : f.listFiles()) {
if (file.isHidden() == false) {
total += countFilesAndFolders(file);
}
}
return total + 1;
}
}
Upvotes: 3
Views: 933
Reputation: 819
A simple JUnit test looks something like this:
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class MyTests {
@Test
public void directoryTest() {
// ExplorerView class is tested
ExplorerView explorer = new ExplorerView();
explorer.countFilesAndFolders("ExplorerViewTestFolder");
// assert statements
assertEquals("filecount must be same ", 12, explorer.getNumberFiles());
assertEquals("directorycount must be same ", 2, explorer.getNumberDirectories());
}
}
If the assertEquals
evaluates to true - if explorer.getNumberFiles() == 12
and explorer.getNumberDirectories() == 2
, then the JUnit tests will pass. Otherwise, they will fail.
Upvotes: 1