I really like dbUnit for unit tests, but I like it more for other
tasks, as exporting data from the database in xml or Excel formats with
a few lines of code and without bothering about writing xml tags or
interacting with POI to write Excel cells.
For
instance the next code can be used to export the result of a query into
an Excel file.
public class Report { private DataSource dataSource; public void exportData() throws Exception { IDatabaseConnection connection = new DatabaseConnection(dataSource .getConnection()); try { // partial database export QueryDataSet partialDataSet = new QueryDataSet(connection); String query = "SELECT a, b FROM t WHERE a = 'whatever'"); partialDataSet.addTable("Report", query); XlsDataSet.write(partialDataSet, new FileOutputStream( "report.xls")); } finally { connection.close(); } }
There’s even a way to integrate Spring’s AbstractTransactionalSpringContextTests with DBUnit. That way, you can insert some test data, do some query testing, and on teardown, the transaction is never comitted. None of your test data remains in the database. Now, that’s cool stuff! 😉
Your work is great. Happy New Year! And Marry Cristmass!