我很难搞清楚如何测试我自己制作并在Project Files中导入的目录。
目前我有:
File file = new File(ExplorerView.class.getResource("/ExplorerViewTestFolder"));
ExplorerView explorer = new ExplorerView();
explorer.countFilesAndFolders("/ExplorerViewTestFolder");
Edit2:已更改为
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;
}
}
答案 0 :(得分:1)
一个简单的JUnit测试看起来像这样:
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());
}
}
如果assertEquals
的计算结果为true - 如果explorer.getNumberFiles() == 12
和explorer.getNumberDirectories() == 2
,那么JUnit测试将通过。否则,他们将失败。