Unit-testing – Unit testing data access objects

database-developmenttddunit testing

I have recently started using test-driven development and unit testing, and it has paid off immensely in the areas where I have aplied it. One area that it has been helpful in is database access. When I abstract away the data access, the testing of methods that need the data become almost ridiculously easy.

However, I haven't been able to find or figure out a way to test the DAOs themselves. (It would be self-defeating to abstract them out!) A DAO should in theory just move data back and forth between the database and the application; can this be considered too simple to test?

I have experimented with setting up a Derby database on my local machine for tests, but it's difficult to automate starting the server, creating the databases, and creating the tables.

Is there any way to automate testing of data access objects?

Best Answer

If you want to automate this sort of testing (I agree with Gary that it's integration rather than unit-testing), have a look at DBunit. It's a framework that allows you to set up a clean database copy for each test, allowing you to run tests against known data in an automated fashion. Combine it with the Hypersonic in-memory database for speed.