This question already has an answer here:

I want to know if the service test classes should use real DAO objects and connect to the database or it should use mock objects to test only the business logic it do?

marked as duplicate by gnat, Kilian Foth, Bart van Ingen Schenau, user40980, Dan Pichelman Feb 18 '14 at 20:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


Your test classes shouldn't connect to the database if you are testing only the business logic, you should use mocks instead.

This way is faster since the tests don't open and close connections and database independent ( you can run the tests without any database. )

If you want to test the DAO objects and the connections to the database then you should have other tests doing that.


As an alternative to Thanos Papathanasiou answer, I've seen some DAO objects unit test using in memory database such as SQLite. It's very fast and safe testing.

An example of it testing nHibernate (ORM) can be found on Ayende blog post. Check the same method with Django.

Not the answer you're looking for? Browse other questions tagged or ask your own question.