Skip to content

TestingThingsThatUseFileObjectDataObjectDataFolder

Antonio Vieiro edited this page Jan 25, 2018 · 1 revision

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 `FileObject`s 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

Apache Migration Information

The content in this page was kindly donated by Oracle Corp. to the Apache Software Foundation.

This page was exported from http://wiki.netbeans.org/TestingThingsThatUseFileObjectDataObjectDataFolder , that was last modified by NetBeans user Geertjan on 2011-05-13T13:08:00Z.

NOTE: This document was automatically converted to the AsciiDoc format on 2018-01-26, and needs to be reviewed.

Clone this wiki locally