Question

I am using DBUnit (version 2.4.9) for loading the data for integration tests. I'm getting a weird problem that 1 field (deleted) is not being set in the DB (postgres).

Here is my XML data load:

<dataset>
    ...
    <workgroup id="100" created="2013-10-08 14:15:00.000" deleted="2013-10-08 14:15:00.000" version="0" name="Name1" org_id="100"/>
    ...
</dataset>

Here is my schema definition:

CREATE TABLE workgroup
(
  deleted timestamp without time zone,
  ... some constraints
)

All other fields are being set-up correctly. Any ideas what can be causing this? Thanks!

EDIT:

I narrowed down the problem and it has to do something with entries in XML file order. If I have:

<workgroup id="101" version="0" name="Name1"/>
<workgroup id="100" version="0" name="Name1" deleted="2013-10-08 />

it doesn't work, but this:

<workgroup id="100" version="0" name="Name1" deleted="2013-10-08 />
<workgroup id="101" version="0" name="Name1"/>

works correctly. DBUnit bug?

EDIT: Another example, that I cannot overcome:

<organisation id="1"/>
<organisation id="2" parent_id="1"/>

Organisation needs to exists before we can assign it value, so workaround, like this:

<organisation id="2" parent_id="1"/>
<organisation id="1"/>

will not work.

Was it helpful?

Solution 2

It appears that this is a "feature" of dbunit. To avoid this behavior you must provide a dtd to define the columns.

<!ATTLIST organisation  
    id CDATA #REQUIRED  
    parent_id CDATA #IMPLIED  
> 

OTHER TIPS

The "feature" of dbUnit is that the first line defines the columns, then the undeclared columns in second line are ignored !

The solution is in 2 steps:

  • have all columns in the first line(also in all previous dbunit files). Like in your workAround
  • use ReplacementDataSet to set "null" in needed columns

the dataset.xml

  <organisation id="1" parent_id="[null]"/>
  <organisation id="2" parent_id="1"/>

AND the trick to set the columns to null in your java test class

@Override
protected IDataSet getDataSet() throws Exception {
  FlatXmlDataSetBuilder flatXmlDataSetBuilder = new FlatXmlDataSetBuilder();
  IDataSet ds = flatXmlDataSetBuilder.build(getClass().getResource("dataset.xml"));
  ds = new ReplacementDataSet(ds);
  ((ReplacementDataSet) ds).addReplacementObject("[null]", null);

  return ds;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top