我正在尝试使用 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]
最佳答案
我刚刚开始使用 vfs,在对依赖于 vfs 的组件进行单元测试时,我采用了使用“ram://”文件系统的方法,而不是尝试完全模拟 VFS 接口(interface)。
这意味着单元测试不再是“纯粹的”,因为测试行为现在不仅仅依赖于 SUT(被测对象),但这是一个妥协,我很乐意接受它,以便让它正常工作.
关于java - Commons-vfs 模拟一个文件系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6916787/