DBUnit Boolean value
Asked Answered
M

1

6

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
Merry answered 26/1, 2015 at 7:3 Comment(0)
I
1

(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>
Intercollegiate answered 26/1, 2015 at 7:9 Comment(13)
I see. So where should I place the value true or false to the column (for example: active)? Should it be written on the dataSet.xml as <Table active="<value>true</value>">? I forgot to mention, I don't have an application-context.xml since I'm using SpringBootMerry
But what if this boolean column can be set to null?Merry
@KaidoShugo You can use <null /> instead of <value>`, see my edited answer for an example.Intercollegiate
After trying your solution for the first attempt. It returned me a NoSuchTableException: value. How is this error possible?Merry
@KaidoShugo Are you sure you followed the example exactly? Because, as you said, you should not get such an exception. Can you please share your xml so I can see what's wrong?Intercollegiate
@KaidoShugo I now see I was missing the closing </table> tag. Please try to add it and check if that was the issue.Intercollegiate
Yes. Anyways, can I ask why is the structure of the dataset if different from the one I've given on my question?Merry
@KaidoShugo Oh, now I see it. You were using FlatDataSet. My example uses XmlDataSet.Intercollegiate
@KaidoShugo I completely missed the problem there. I edited my post with a solution for FlatDataSet, which also explained what the issue was.Intercollegiate
So is the FlatXmlDataSetBuilder going to be declared a bean? Or is it a standalone?Merry
@KaidoShugo that's completely orthogonal to the problem - as long as you can control setColumnSensing(true) (e.g., by property injection in a bean), you should be fine.Intercollegiate
I'll try to create a sample project repository for reference. Maybe you could help me there.Merry
I have placed a Bitbucket repository, hope you could see it soon.Merry

© 2022 - 2024 — McMap. All rights reserved.