Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

marked as duplicate by gnat, Kilian Foth, Bart van Ingen Schenau, MichaelT, 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.

up vote 3 down vote accepted

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.

share|improve this answer

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.

share|improve this answer

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