Commons-vfs模拟文件系统

时间:2011-08-02 18:29:47

标签: java apache-commons-vfs

我正在尝试使用commons-vfs作为文件系统包装器,以便更轻松地对需要触摸文件系统的一些代码进行单元测试。现在我刚刚熟悉API。我想要做的是创建一个虚拟文件系统,并添加几个文件(一个文件夹,然后该文件夹中的文件到根目录)。

这是我写给testdrive 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 个答案:

答案 0 :(得分:1)

我刚刚开始使用vfs,并且在依赖于vfs的组件的单元测试中,我采用了使用“ram://”文件系统而不是尝试完全模拟VFS接口的方法。

这意味着单元测试不再是“纯粹的”,因为测试行为现在不仅仅依赖于SUT(受测试的主体),而且这是一种妥协,我很乐意为了方便工作而生活