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 |
|---|---|
| FlatXmlDataFileLoader | FlatXmlDataSet |
| FullXmlDataFileLoader | XmlDataSet |
| CsvDataFileLoader | CsvDataSet (via CsvURLDataSet) |
| XlsDataFileLoader | XlsDataSet |
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):
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.


