5

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?

Mohamed Ramadan
  • 679
  • 1
  • 7
  • 12

2 Answers2

5

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.

Thanos Papathanasiou
  • 2,462
  • 1
  • 20
  • 22
2

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.