2015-11-20 52 views
3

我很难搞清楚如何测试我自己创建并导入到工程文件中的目录。JUnit测试用例需要测试目录

目前我有:

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

你用'测试目录'是什么意思?你有任何示例代码? – jiaweizhang

+0

当你试图运行你现有的程序(从昨天开始,对吧?)会发生什么? – jiaweizhang

+0

@家威章一切运行良好据我所知!我只需要做一些测试用例。 – ProjectDefy

回答

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计算为真 - 如果explorer.getNumberFiles() == 12explorer.getNumberDirectories() == 2,然后JUnit测试将传递。否则,他们会失败。

+0

我收到一个错误。我去了,并将它添加到主帖子中。 (对不起,如果我做这个stackoverflow的事情是错误的)。 – ProjectDefy

+0

啊。轻松修复。它期望一个'File',但你给它一个'String'。所以,只需要使用'File f = new File(“/ ExplorerViewTestFolder”)''做我们昨天所做的。所以这行应该是:'explorer.countFilesAndFolders(new File(“/ ExplorerViewTestFolder”));' – jiaweizhang

+0

我试过了,除非我读错了你哈哈。在这里,我做了一个更新编号3,它给了我一些其他的东西。 – ProjectDefy