[英]JUnit Test Case need to test a directory
I'm having difficult time figuring out how to test a directory that I've made myself and imported in the Project Files. 我很难搞清楚如何测试我自己制作并在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 Edit2:改为
public class ExplorerViewTests {
@Test
public void testCountFilesAndFolders() {
ExplorerView explorer = new ExplorerView();
explorer.countFilesAndFolders("/ExplorerViewTestFolder");
}
EDIT4: 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;
}
}
A simple JUnit test looks something like this: 一个简单的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());
}
}
If the assertEquals
evaluates to true - if explorer.getNumberFiles() == 12
and explorer.getNumberDirectories() == 2
, then the JUnit tests will pass. 如果assertEquals
计算结果为true - 如果explorer.getNumberFiles() == 12
和explorer.getNumberDirectories() == 2
,那么JUnit测试将通过。 Otherwise, they will fail. 否则,他们将失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.