Friday, December 6, 2013

DbUnit WARN in logs "Extra columns on line 2. Those columns will be ignored."

WARN dbunit.dataset.xml.FlatXmlProducer - Extra columns on line 2.  Those columns will be ignored.
WARN dbunit.dataset.xml.FlatXmlProducer - Please add the extra columns to line 1, or use a DTD to make sure the value of those columns are populated or specify 'columnSensing=true' for your FlatXmlProducer.
WARN dbunit.dataset.xml.FlatXmlProducer - See FAQ for more details.

Problem was in XML file which defined data that should be loaded to database. Here is simplified version:

 <user id_user="1"/>

 <user id_user="2" login="john" />

Problem was in parameter columnSensing. During reading XML data with FlatXmlProducer parameter columnSensing was defined as false or wasn't defined which is treated as false. Let's look at meaning of columnSensing parameter.

This parameter define which columns will be inserted into tables. When parameter columnSensing is falseDbUnit reads xml elements defining data one by one. When new element is detected that all attribute are considered as column names. Next time when element with same name is found inserted into database table are just already columns. When columnSensing is true than columns will be inserted into database table even if they are not defined in first XML element.

Solution 1.

First solution of previous example is quite easy just set columnSensing parameter to true.

Solution 2.

It's also really straightforward reorder XML elements:

 <user id_user="2" login="john" />

 <user id_user="1"/>

Solution 3.

In more complex XML files previous solutions can't be used, so define in first XML element as null. So it should like this:

 <user id_user="1" login ="[NULL]"/>

 <user id_user="2" login="john" />

DbUnit doesn't understand null value as null value. There have to be piece of code that learn DbUnit [NULL] value:

IDataSet dataSet = new FlatXmlDataSet(new FileInputStream("dataset.xml"));
ReplacementDataSet finalDataSet = new ReplacementDataSet(dataSet);
finalDataSet.addReplacementObject("[NULL]", null);

I don't know if it's DbUnit error or some misinterpretation of my XML file but DbUnit detect problem at line 1 or 2 even if real problem occurs many lines after.

No comments:

Post a Comment