Data File Loader

Nearly all tests need to load data from one or more files, particularly for prep or expected data. org.dbunit.util.fileloader has a set of data file loader classes that load datasets from files on the classpath, combining classpath-resource reading with ReplacementDataSet substitution — every dataset a loader returns has already had its configured replacement objects/substrings applied.

Loader Wraps
CsvDataFileLoader CsvDataSet (via CsvURLDataSet)
FlatXmlDataFileLoader FlatXmlDataSet
FullXmlDataFileLoader XmlDataSet
JsonDataFileLoader JsonDataSet
XlsDataFileLoader XlsDataSet
YamlDataFileLoader YamlDataSet

Dispatching By File Extension

FileExtensionDataFileLoader picks the loader to delegate to from the file’s extension, so one loader instance can load a mix of formats:

Extension Delegate
.json JsonDataFileLoader
.xls, .xlsx XlsDataFileLoader
.xml FlatXmlDataFileLoader
.yaml, .yml YamlDataFileLoader
DataFileLoader loader = new FileExtensionDataFileLoader();
IDataSet xmlDs = loader.load("/the/package/prepData.xml");
IDataSet jsonDs = loader.load("/the/package/prepData.json");

The delegate loaders are used only for their format parsing; replacement objects and substrings configured on the FileExtensionDataFileLoader itself are the ones applied, exactly once, regardless of which delegate handled the file.

An unsupported extension throws IllegalArgumentException naming it. Two formats are deliberately not dispatched here:

  • CSV — dbUnit’s CSV format is directory-based (CsvURLDataSet expects a directory containing table-ordering.txt), so it cannot share this loader’s one-file-per-path convention. Use CsvDataFileLoader directly, pointed at a directory.
  • Full (non-flat) XML.xml maps to flat XML, the overwhelmingly common case. Use FullXmlDataFileLoader directly for the full XML format.

Each extends AbstractDataFileLoader, which implements the shared DataFileLoader contract: load(String filename) resolves filename as a classpath resource (via Class.getResource()), delegates the actual parsing to the wrapped format, then wraps the result in a ReplacementDataSet. A null/empty filename returns an empty DefaultDataSet rather than throwing.

A simple usage example:

DataFileLoader loader = new FlatXmlDataFileLoader();
IDataSet ds = loader.load("/the/package/prepData.xml");

Replacement Objects and Substrings

Every loader’s constructors accept replacement object and replacement substring maps, used the same way as with ReplacementDataSet directly (see Decorators, and Dataset Values for the [NULL] convention):

Map<String, Object> replacementObjects = new HashMap<>();
replacementObjects.put("[NULL]", null);

DataFileLoader loader = new FlatXmlDataFileLoader(replacementObjects);
IDataSet ds = loader.load("/the/package/prepData.xml");

You can also add or clear replacements after construction: addReplacementObjects(), addReplacementSubstrings(), removeAllReplacementObjects(), removeAllReplacementSubstrings().

See PrepAndExpectedTestCase for a DataFileLoader used as part of a full prep/expected test setup.