Cookie Policy
We may store information about you using cookies (files which are sent by us to your computer or other access device) which
we can access when you visit our site in future. We do this to enhance user experience. If you want to delete any cookies that
are already on your computer, please refer to the instructions for your file management software to locate the file or directory
that stores cookies. Our cookies will have the file names symfony, JSESSIONID, IS and ISCOOKIE. Information on deleting or controlling
cookies is available at www.AboutCookies.org. Please note that by
deleting our cookies or disabling future cookies you may not be able to access certain areas or features of our site.

DBUnit + Hibernate Integration testing

DBUnit is a great integration testing tool for projects that use databases as persintance layer. It is an extension for Junit, which gives all the goodies of JUnit and a powerful mechanism to test your database and persistance services. DBUnit provides easy way to initialise your database, populate initial data and create/restore consistent state for each test.

The purpose of this article is however not to give overview of DBUnit but rather give some basic guidance on how this testing framework could be integrated into projects that are based on hibernate.

So the plan is to make DBUnit use the connection provided by hibernate session to allow testing of the persistence layer services and still make use of the DBUnit's mechanism of database consistent state initialization.

1. In the constructor we define the system properties that dbUnit will use for connection.

2. In the #setUp() we start the server, initialize the session factory (which will recreate the database using hbm2dll) and call the super.setUp() of DBTestCase which will load the initial data using the #getDataSet() method which will define the data set to use for this test suite. You probably will need to define a different data set for each of test suite's but it is placed here for the demo.

3. In the #tearDown() we close the hibernate session run the DBUnit stuff by calling super.tearDown() and shutdown the hsqldb server.

4. Notice the getSetUpOperation() and getTearDownOperation() these two are instructions for the DBUnit on how to prepare the database for the current test. REFRESH means that we need to recreate all data from scratch before we run the test and NONE means that we want to do nothing after the test is run (The none in after this makes no sense for the HSQLDB but if you use some other database this will allow you to view the data after the test has been run, so that you can see what is there for your self)

Some additional notes: probably it is not a good idea to start and shutdown the HSQLDB server each test but this is just a demo, so you probably will need to do some additional thinking on how you want your integration test to run.

Now all we need to do is to extend our HibernateDbUnitTestCase class that write some tests. As you can see what we do is that is the constructor of the test case we start the HSQLDB Server and initialise the hibernate sesstion factory. Then we open the session and provide connection for DBUnit.

There is a special hook method that is this test case. The purpose of it is to initialise the fresh database that hibernate had created for us with data. And this is were DUnit gives us a great help by loading the data from flat XML files. So that all test that we write may provide their own data that is needed just for the test.

Finally in finalize method we close all our connections and shutdown the HSQLDB server.