2011-08-02 87 views
3

我試圖使用commons-vfs作爲文件系統的包裝,以便更容易地單元測試一些需要接觸文件系統的代碼。現在我只是熟悉API。我想要做的是創建一個虛擬文件系統,並添加一些文件(文件夾,然後將該文件夾中的文件添加到根目錄)。Commons-vfs嘲諷一個文件系統

這是一個測試類,我寫來測試驅動的API:

public class CommonsVfsLearningSpikeTest extends Base { 
FileSystemManager fsManager; 
FileObject rootVFS; 

@Before public void createFixture() throws Exception{ 
    this.fsManager = VFS.getManager(); 
    this.rootVFS = fsManager.createVirtualFileSystem("rootVfs"); 
} 

@Test public void testCreationOfDefaultFileSystem() throws Exception { 
    assertNotNull(fsManager); 
} 

@Test public void testCreationOfVFS() throws Exception { 
    //root file has an empty base name 
    assertEquals("", rootVFS.getName().getBaseName()); 
} 

@Test public void testCreationOfChildrenFiles() throws Exception { 
    FileObject childFolder = rootVFS.resolveFile("childFolder"); 
    childFolder.createFolder(); 
    assertNotNull(childFolder); 

    FileObject childFile = rootVFS.resolveFile("childFolder/childFile"); 
    childFile.createFile(); 
    assertNotNull(childFile); 

} 

}

目前,我發現了以下錯誤:

[junit] Testcase: testCreationOfChildrenFiles(com.usengineeringsolutions.bridgewatch.vfs.CommonsVfsLearningSpikeTest):  Caused an ERROR 
[junit] Incorrect file system URI "file:///" in name "file:///rootVfs/childFolder", was expecting "/rootVfs/". 
[junit] org.apache.commons.vfs.FileSystemException: Incorrect file system URI "file:///" in name "file:///rootVfs/childFolder", was expecting "/rootVfs/". 
[junit]  at org.apache.commons.vfs.provider.AbstractFileSystem.resolveFile(AbstractFileSystem.java:274) 
[junit]  at org.apache.commons.vfs.provider.AbstractFileSystem.resolveFile(AbstractFileSystem.java:267) 
[junit]  at org.apache.commons.vfs.provider.AbstractFileObject.resolveFile(AbstractFileObject.java:670) 
[junit]  at com.usengineeringsolutions.bridgewatch.vfs.CommonsVfsLearningSpikeTest.testCreationOfChildrenFiles(CommonsVfsLearningSpikeTest.java:27) 
[junit] 
[junit] 

回答

1

我剛剛開始使用vfs以及在依賴於vfs的組件的單元測試中,我採用了使用「ram://」文件系統的方法,而不是試圖完全模擬VFS接口。

這意味着單元測試不再是「純粹」的,因爲測試行爲現在不僅僅依賴於SUT(被測試者),而是一種妥協,我很樂意爲獲得它而採取適當的妥協措施加工。