Saturday, May 5, 2012

DBUnit in Practice

For those of you unfamiliar with DBUnit, it works with JUnit to put a database into a known state between unit tests (e.g. you can wipe out the DB and start from a blank on each run if you want).

I've recently picked up how to use this technology in practice, and this article will help others ease the (somewhat steep) learning curve.

I found DBUnit to be a very collaborative technology, in the sense that it interacts with a lot of other technologies to do it's job.  First off, DBUnit does not PROVIDE the stub or mock database, you'll have to provide one yourself (Apache Derby and HyperSQL are swell for this).  Secondly you have to establish a connection to the DB yourself (using JDBC) and thirdly you'll need to create the tables for the DBUnit data yourself (e.g. via SQL CREATE TABLE statements).  Lastly you'll need to dependency inject (if you know Spring than this concept is nothing new) your JDBC connection object going to the test DB into whatever class you are going to test.  THEN you can run your DBUnit tests.

In conclusion, you need to use a lot of other technologies and paradigms in order to truly use DBUnit.  I hope that this overview and the links help.

No comments:

Post a Comment