dbunit and null values

For testing database based applications with more or less fixed schema dbunit is nice for creating test data. There are several ways of defining the data in xml and loading it into the db. With the FlatXmlDataSet it looks like

The first row defines the schema for dbunit with this format. When you have self referencing data with columns linked to the pk column it gets more complicated. Think of a growing company with locations all over the world. The hq will be the primary location and all other belong to their regional hq which belongs to the global hq. In the table is a column parent_location as fk to the id column. So the global hq has no parent and all other rows have parents. With the above sample data all rows has null as value in the parent column. DBUnit’s FlatXmlDataSet ignores all other column data in subsequential rows if they are not mentioned in the first row of a table. To avoid this behavior you must provide a dtd inline or external to define the columns:



Now the first row has a null parent value and all others the value defined in the XML file.