JUnit测试用例需要测试一个目录

时间:2015-11-20 00:08:46

标签: java junit directory

我很难搞清楚如何测试我自己制作并在Project Files中导入的目录。

目前我有:

File file = new File(ExplorerView.class.getResource("/ExplorerViewTestFolder"));

我试图从这里尝试来测试Picture

ExplorerView explorer = new ExplorerView();

explorer.countFilesAndFolders("/ExplorerViewTestFolder");

Edit2:已更改为

public class ExplorerViewTests {

@Test
public void testCountFilesAndFolders() {

    ExplorerView explorer = new ExplorerView();

    explorer.countFilesAndFolders("/ExplorerViewTestFolder");


}

错误:enter image description here

EDIT3:enter image description here

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;
    }
}

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() == 12explorer.getNumberDirectories() == 2,那么JUnit测试将通过。否则,他们将失败。