unit-testingdaodata-access-object

Testing Data Access Objects


When testing a DAO i follow these rules:

This works well for selecting from the database. I setup the database with the data needed for the select, call my DAO and verify, that the returned object has the right values.

But when testing insert, update and delete it gets ugly. I have to write a custom select statement to verify, that the correct data was inserted/updated/deleted in my database. So when im finished writing the tests i could just aswell test my tests again.

Some people on the web suggest mocking literally everything, but that doesn't really test anything imo.

So, how does one test a DAO?


Solution

  • You don't really have to test DAO, provided they are slim enough (as they should be) and don't contain business logic. You're going to inadvertently test them at some point while writing some system/integration tests for something else.

    Depending on what you're using (ORM framework?) you might be able to stub/mock stuff, but it's rarely worth it.

    As for using connection strings in NUnit (so to manipulate the DB from your test project), I see nothing wrong with that in general, it's more common than you'd think.