Thursday, September 24, 2009

DBUnit and Foriegn Key Constraints


DB Unit is great, except for the FK constraint hell you go through until you learn to do..
1. IDataSet dataset = new FlatXmlDataSet(
ClassLoader.getSystemResourceAsStream(
datasetFilename));
2. dataset = new FilteredDataSet(
new DatabaseSequenceFilter(
getConnection()), dataset);
Line 2 there looks at the constraints and the dataset and determines the correct ordering for the SQL statements for your database, so that when they're run in the setup/teardown of your DBUnit tests you don't get FK constraint violations.

There are many blog postings that give all kinds of ways to disable or defer FK checks by the database, but this works better I think, for me since I have the test setup in Java. This came from the comments in a Matt Raible post from 2006.

No comments: