【问题标题】:Commons-vfs mocking up a filesystemCommons-vfs 模拟文件系统
【发布时间】:2011-08-02 18:29:47
【问题描述】:

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

这是我编写的用于测试 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]

【问题讨论】:

    标签: java apache-commons-vfs


    【解决方案1】:

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

    这意味着单元测试不再是“纯粹的”,因为测试行为现在不仅仅依赖于 SUT(被测对象),但这是我很乐意接受的一种折衷方案,以便于使其正常工作.

    【讨论】:

      猜你喜欢
      • 2010-11-23
      • 1970-01-01
      • 2020-01-28
      • 1970-01-01
      • 2012-04-02
      • 1970-01-01
      • 2012-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多