Manage test data for integration tests using Spring and DBUnit


    Testing is very important to application development. Most enterprise applications are related with persistence data in database system. If you have to test integration between DAOs and databases, called Data Access Objects Testing (DAOs Testing), you will prepare data in database for these testing. There are many ways to prepare test data in database.

  • 1. Inserting test data using SQL script by manually.
  • 2. Import and Export data automatically by other applications.


DBUnit is a tool which provides for Data Access Objects Testing.



Process of DBUnit

    DBUnit export set of data in the one database to flat file (XML) before running the test. Then the tests are run, it import these test data from XML file to another database and deletes the test data when the tests are finish.


Spring Framework and DBUnit

    Spring framework provides ?AbstractTransactionalDataSourceSpringContextTests? class for DBUnit. This class has ?onSetUpInTransaction? and ?onTearDownInTransaction? methods which can be overridden to use to insert and delete test data. Spring-managed beans defined in the integration tests are injected into the integration tests. Junit 4 and TestNG provide a more sophisticated lifecycle.


Advantages of DBUnit

    1. Simple and easy to implement.
    2. Test data of each tests is independent because it insert the test data when the tests is run and delete when tests is finished
    3. Support Spring framework, objects is injected automatically.
    4. XML file, that is created to store data, as a template and developer can modify its.


Reference : http://www.theserverside.com/tt/articles/article.tss?l=ManageTestDataSpringandDBunit

Leave comment...