Announcement Announcement Module
Collapse
No announcement yet.
Unit testing framework with Spring and Hibernate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unit testing framework with Spring and Hibernate

    Here is the deal. I am trying to write a unit testing framework for the company that I work for. We're trying to write this at a low level, starting at the security framework, where all of the user management, etc. is done. There three main security components. One that deals with database access, which is done through hibernate. Another component that deals with user management. The third is the authentication component that logs users into the application. I am just wondering if any of you have done anything similar and can provide any insight as to how you accomplished this in terms of general procedure and data management. Thus far I have examined a few options.

    1. Using HSQLDB and running an in memory database. All of the data would be managed with DBUnit. The main issue we have found is that HSQLDB does not let you run Oracle dialect specific SQL using keywords such as START WITH and CONNECT BY. This would mean that not all our unit tests could be tested with this approach, and some would have to go out to the actual database. As the code base grows, the unit tests that are unable to be run against HSQLDB might increase exponentially, and this could potentially be bad.

    2. Using HSQLDB and the in memory database for the low level tests, such as testing main database access and user management functionality. However, for anything that goes above the user management layer we would provide a test facade with mock objects. This would allow for the ability to run any test cases as these mock objects are completely managed by us.

    It would be great if someone could provide some comments/criticism on these approaches, maybe from personal experience. Also, if you have any other approaches, please let me know.

    Thanks!

  • #2
    I've tried using HSQLDB for unit tests (or rather function tests..) but stumbled upon some cases where some features were different or missing in the target production database (or the other way around). Since then I always run the tests against the production database, (such as MYSQL) locally installed on my workstation).

    In the current project (using a legacy JDO implementation) we use DBUnit, I prefer doing it programmatically though, instantiating objects and then persisting them as describe in many examples on unit testing DAO's (see Spring doc's).

    DBUnit is pretty good at importing and exporting test data and can be used as a means to do that for integration testing etc.

    /S
    Kentor, Sweden

    Comment


    • #3
      It's a difficult one. If you use the real database for tests, they could take took long to run often. We ran into the problem when I added that many tests the coverage went up but so did the execution time. Nearly an hour for the whole suite. Switching that out for an in-memory database, they ran soooo much quicker. The best thing we found was to run what you can against in memory, anything that you can't has to be run against a real one. We also used to run the CI system against a real one to catch problems.

      I've also started playing with H2 after Costin mentioned it.
      http://www.h2database.com/html/frame.html

      Comment


      • #4
        Originally posted by karldmoore View Post
        It's a difficult one. If you use the real database for tests, they could take took long to run often. We ran into the problem when I added that many tests the coverage went up but so did the execution time. Nearly an hour for the whole suite. Switching that out for an in-memory database, they ran soooo much quicker. The best thing we found was to run what you can against in memory, anything that you can't has to be run against a real one. We also used to run the CI system against a real one to catch problems.

        With real database did you mean the production database located on the production server or a locally installed one on your workstation? I use the later approach and tests run quickly enough.

        I've also started playing with H2 after Costin mentioned it.
        http://www.h2database.com/html/frame.html
        Haven't tried that one yet.

        /S

        Comment


        • #5
          Originally posted by SigmundL View Post
          Haven't tried that one yet.
          I've only just started to try and play with H2. See how it goes, obviously the graphs are slightly biased but it does seem to have more of a complete DB implementation. Lets see shall we.

          Comment


          • #6
            Not sure that what exactly you were looking for.

            I did my unit testing to all my DAO classes by using spring framework's 'AbstractTransactionalDataSourceSpringContextTests '-class and with a "real" DB - inmemory based HSQLDB database. I used separate XML bean (DAO) configuration files for testing.

            AbstractTransactionalDataSourceSpringContextTests-class contains transactions and with rollback functionality (by default) which is great for testing purposes.

            The rest of the code in my application I tested by stubs and mocks.

            Comment


            • #7
              Im having a small problem with the aforesaid approach..


              I use AbstractDep....Tests and (in-mem) HSQLDB, for testing the dao. Consider the following case .

              We need to save a User to the database who has many roles. (many-to-many relation).

              I need to run the tests for this as independent test , other than from ant.

              First i run a unit test for role to make sure that theres atleast one role in the table

              Now from the unit test for User when i issue a statement


              List<Role> listOfRoles = roleDAO.getByQuery(_HQL_GET_ADMIN_ROLE);

              I dont get back any roles and the count is 0.

              But when I run the entire cases from ant it works..

              So i guess that this is something to do with the hibernate session..


              Ive set this property in hibernate.cfg.xml

              <property name="hibernate.hbm2ddl.auto">create</property>

              My questions are

              1) Is there any way to make sure that the data we inserted stays there ?

              2) Is there any way to set the order in which my test cases have to be run in Junit ?

              Comment


              • #8
                I like the DBUnit approach very much but have been forced to abandon it when my DB models have grown, especially with Hibernate managed relations. There are, to put it mildly, a constraint violation hell!!! And that's a pity. I saw a tip at the forum for another approach; to simply use AbstractTransactionalDataSourceSpringContextTests as a base for DB tests and have it rollback any changes made. Tried it, and it worked ok I guess, but I still would prefer the CLEAN_INSERT of the DBUnit framework if that could work properly.

                The H2 sounds interesting. Are there a HibernateDialect for it yet?
                Last edited by kantorn; Feb 24th, 2007, 05:19 AM.

                Comment


                • #9
                  Originally posted by kantorn View Post
                  The H2 sounds interesting. Are there a HibernateDialect for it yet?
                  I've not tried it yet, but it does look like there is.
                  http://www.hibernate.org/hib_docs/v3...H2Dialect.html

                  Comment


                  • #10
                    Originally posted by kantorn View Post
                    I like the DBUnit approach very much but have been forced to abandon it when my DB models have grown, especially with Hibernate managed relations. There are, to put it mildly, a constraint violation hell!!! And that's a pity. I saw a tip at the forum for another approach; to simply use AbstractTransactionalDataSourceSpringContextTests as a base for DB tests and have it rollback any changes made. Tried it, and it worked ok I guess, but I still would prefer the CLEAN_INSERT of the DBUnit framework if that could work properly.
                    I prefer ATDSSCT over DbUnit these days. I have a separate app context XML for testing. I use the Hibernate create task in my sessionFactory properties to create the schema for me. I populate the data in the onSetUpInTransaction() method. I run all my tests, then the whole thing is rolled back. That way I always know that the data on which my tests depend are in the database.

                    I'm using HSQL. I have complete control that way. With Hibernate it's easy enough to migrate to Oracle when I go to production.

                    %

                    Comment


                    • #11
                      Nice Benchmark for H2

                      Originally posted by karldmoore View Post
                      I've not tried it yet, but it does look like there is.
                      http://www.hibernate.org/hib_docs/v3...H2Dialect.html
                      Nice looking benchmark results for H2. I might be another user taken in by a barchart, but the docs look interesting. Thanks for pointing this out.

                      %

                      Comment


                      • #12
                        Originally posted by duffymo View Post
                        Nice looking benchmark results for H2. I might be another user taken in by a barchart, but the docs look interesting. Thanks for pointing this out.
                        I'm a sucker for a graph as well . I guess it's probably biased in thier favour, but then again it might just be that good. I actually thought the feature set was quite impressive. Especially the support for clustering and transaction isolation. I'm hoping to have a proper play with it at some point so I'll have to wait and see.

                        Comment

                        Working...
                        X