Recently I have been using [DBUnit][1] to run my DAOs unit test with Spring and HSQL in memory database. One of the biggest challenge in testing the DAO layer is generating data to test with. Of course you could export data in your development database in style of insert statement files and import them into the test database before executing the test. But I think doing so still requires a lot of manual work for a busy developer. One nice thing about DbUnit is that you can export your test data into a single XML file and while setting up the unit test, you tell DbUnit to execute the import based on the XML data file.
Just for reference, a FlatXmlDataSet looks like this
1 2 3 4 5 6 7 |
|
DbUnit will create 2 rows for table1 and 2 rows for table2 in your schema. For more info please see this Now Groovy Builder, especially the MarkupBuilder is perfect for this kind of task: extract data from a query and build the XML file based on the extracted data. The code looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
For each query the map ‘queries’ define, MarkupBuilder will generate the FlatXmlDataSet format entries for the returned tuples. There we go your test data file!