Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DBUnit Boolean value

After learning SpringBoot, I wanted go further to handle integration tests using (DBUnit and SpringTestDBUnit). Throughout the process, everything was going well until I came across setting values for boolean datatyped columns on the dataset. (Contents of the dataset is given below)

<?xml version="1.0" encoding="UTF-8"?>
<dataset>
    <Client code="0001" name="client_one" />
    <Client code="0002" name="client_two" />
    <Client code="0003" name="client_three" active="false" />
    <Client code="0004" name="client_four" />
</dataset>

Adding active="false" attribute to Client record [code=0003], my integration tests fails and showing me this message Exception processing table name='Client' which was resulted the Client record [code=0001] violates the active not null column constraint.

After fixing the error (on branch DBUnit_For_Boolean_Columns_Attempt_One) by supplying values for active column on all records (which is a bit off the specification), it worked. But my target was able to run the integration tests successfully with the dataset written above.

The question is how can the integration tests be successful using the dataset above? As of now, I'm having a hard time implementing solutions so I've created a Bitbucket repository for you to see and help on-hand.


Changelogs

  • 2015/02/04 changes

    1. Improve question contents
    2. Added Bitbucket repository
like image 332
David B Avatar asked Jan 26 '15 07:01

David B


1 Answers

(I had a different answer here, which came from a complete misdiagnosis of the issue, as I missed the fact OP was using FlatXmlDataSet).

From FlatXmlDataSet's documentation:

Table metadata is deduced from the first row of each table by default. Beware that DbUnit may think a table misses some columns if the first row of that table has one or more null values.

This seems to be the issue here, as the first entry does not specify a value for active. To avoid this problem, you can use DBUnit's column sensing property, which essentially makes DBUnit read the entire XML before deducing the table's structure:

FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder();
builder.setInputSource(new File("path/to/dataSet.xml"));
builder.setColumnSensing(true); // HERE!
IDataSet dataSet = builder.build();

Alternatively, if you aren't creating the builder yourself and do not want to manipulate it, just make sure to have the active column in every element, especially the first one:

<dataset>
    <client code="0001" name="client_one" active="false" />
    <client code="0002" name="client_two" active="true" />
</dataset>
like image 99
Mureinik Avatar answered Sep 30 '22 15:09

Mureinik