Announcement Announcement Module
Collapse
No announcement yet.
unit testing HibernateDaoSupport Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • unit testing HibernateDaoSupport

    hello, i'm using junit 4 and hibernate 3
    this is my class
    Code:
    public class HibernateUserDAO extends HibernateDaoSupport implements UserDAO {
    
    	public void insert(User user) {
    		getHibernateTemplate().save(user);
    	}
    
    }
    sincechibernate template is a concrete class,
    how do i write a unit test for this class?

  • #2
    Instead of trying to mock the HibernateTemplate, wouldn't you mock the Dao instead? You are using an interface so that should be easy enough.

    Comment


    • #3
      Or if you want to test the implementation itself (seems reasonable) use a database. There is an interface for HibernateTemplate (HibernateOperations) if you want to go down the Mock route. However, most people prefer to test data layer implementations against a real database - if you use the Spring integration test base classes and keep the same configuration for all data layer tests you should find that it is very fast. If your database requirements are simple enough you might also be able to embed an equivalent database in memory (HSQLDB or Derby), to make your tests more portable.

      Comment


      • #4
        Originally posted by david_syer View Post
        Or if you want to test the implementation itself (seems reasonable) use a database. There is an interface for HibernateTemplate (HibernateOperations) if you want to go down the Mock route. However, most people prefer to test data layer implementations against a real database - if you use the Spring integration test base classes and keep the same configuration for all data layer tests you should find that it is very fast.
        There have been a couple of thread recently which talked about testing at the HibernateTemplate level. I've always gone for testing at the Dao level against an in-memory database. Am I missing a trick here? Is there any time you would want to test at the HibernateTemplate level?

        Originally posted by david_syer View Post
        If your database requirements are simple enough you might also be able to embed an equivalent database in memory (HSQLDB or Derby), to make your tests more portable.
        That's the way I've usually gone. We switched from SQL Server to HSQLDB for developer testing, this meant we had no excuse for not running the test suite, it was finished in no time.

        Comment


        • #5
          I don't think there is much value in mocking the template, I merely mentioned it as an option because I know that some people do it (I never understood why). In memory databases are not feature compatible with production platforms, so they can be limiting. The most important thing for me is being able to control (and automate) the contents of the database for test purposes.

          Comment


          • #6
            Originally posted by david_syer View Post
            I don't think there is much value in mocking the template, I merely mentioned it as an option because I know that some people do it (I never understood why).
            Thanks for that David, I would agree with your comments, just wanted to check if there was something I was missing here. I think I'll stick with what I've got.

            Originally posted by david_syer View Post
            In memory databases are not feature compatible with production platforms, so they can be limiting. The most important thing for me is being able to control (and automate) the contents of the database for test purposes.
            The biggest issue we had was with the execution time of the unit tests. Against SQL Server it just took too long. Running against HSQLDB (or a similar database) made TDD actually possible for us, there was no real excuse for breaking the system. Of course there were some tests that we couldn't use with said database, these were run by the CI environment against the production platform. Seemed to work out quite nicely.

            Comment


            • #7
              mocking hibernate template

              Using mockito:


              Code:
              public class HibernateUserDAO extends HibernateDaoSupport implements UserDAO {
              
              	public void insert(User user) {
              		getHibernateTemplate().save(user);
              	}
              
              }

              test class

              Code:
               public void testCreation()
                  {
              	HibernateUserDAO dao = new HibernateUserDAO ();
              	
              	
              	User newEntity = new User();
              	newEntity.setName("whatever");
              	
                      HibernateTemplate mockTemplate = mock(HibernateTemplate.class);
              	when(mockTemplate.save(anyObject())).thenReturn((long)1);
              
              	dao.setHibernateTemplate(mockTemplate);
              	
              	dao.create(newEntity);
              	verify(mockTemplate).save(newEntity);
                  }


              This can help to unit tests the dao, but is not enough.
              You would not be testing the hibernate mapping, spring files, etc. So you also need an integration test that uses a database.

              Comment

              Working...
              X