Announcement Announcement Module
Collapse
No announcement yet.
Testing JdbcDaoSupport subclasses with junit Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Testing JdbcDaoSupport subclasses with junit

    I need some JUnit advice for testing my JdbcDaoSupport sub classes.

    My methods inside my classes call the getConnection() method from JdbcDaoSupport. My first thought was to subclass my class and overload the getConnection() method with one that would return a mock connection. This does not work because the getConnection() method is final. My next thought was to provide a mock datasource to
    JdbcDaoSupport through the setDataSource() method. The mock datasource would then return a mock connection. This also does not work because the setDataSource() method on JdbcDaoSupport calls many methods on the datasource and I can't implement those on my mock. I then though about subclassing again and overloading setDataSource() but it too is final.

    So...How is everyone unit testing their JDBC DAOs in isolation from a DB? Am I missing something obvious?

    Thanks

  • #2
    Mostly DAO testing is not done using mocks. Using a data base is 'always best' because it also acts like a (very) basic integration test. Best would be using the production DBMS (database management system). In a Hibernate project I prefer the HSQLDB database.


    Cheers,

    Martin (Kersten)

    Comment


    • #3
      David,

      you might like to check out the org.springframework.test package. It contains superclasses that create and rollback a transaction and can help get the DB in a known state and run tests.

      Also search for AbstractTransactionalSpringContextTests and AbstractTransactionalDataSourceSpringContextTests in the forums.

      Rgrds, Thomas

      Comment

      Working...
      X