TestingThingsThatUseFileObjectDataObjectDataFolder

Testing Things That Use FileObject/DataObject/DataFolder

If your unit tests use FileObject (including DataObject or DataFolder), then you may be suprised that FileUtil.toFileObject(java.util.File) returns null. This is because the MasterFS filesystem implementation is what maps FileObjects to your local disk and it needs to be on the classpath when tests are run.

See UsingFileSystemsMasterfs for more info on how to fix this.

If for some reason you prefer not to use MasterFS, you can create a new LocalFileSystem, create some files and use that instead of FileUtil.toFileObject in your test. For example, in a NbTestCase subclass:

FileObject dir;
public @Override void setUp() throws Exception {
  super.setUp();
  clearWorkDir();
  LocalFileSystem fs = new LocalFileSystem();
  fs.setRootDirectory(getWorkDir());
  dir = fs.getRoot();
}

If your test just needs some simple data in a FileObject or two, you can avoid writing to disk at all as follows:

FileObject dir;
public @Override void setUp() throws Exception {
  super.setUp();
  dir = FileUtil.createMemoryFileSystem().getRoot();
  //write out data your tests will use to files under dir/ here
}

If you want to write tests for a DataObject or DataLoader, you may also want to set the mime type correctly: DevFaqTestDataObject

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo