DbUnit Framework

DbUnit extends the popular JUnit test framework and is a useful and powerful tool for simplifying unit testing of database operation.
The core components of DBUnit are:

  • IDatabaseConnection
  • IdatabaseConnection
       
       
       
       
       
       
       
       
       
    • DatabaseConnection - wraps a JDBC connection
    • DatabaseDataSourceConnection - wraps a JDBC Data Source
  • IDataSet
  • This is the primary abstraction used by DbUnit to manipulate tabular data. Commonly used implementations are:
    • FlatXmlDataSet
    • DefaultDataSet
    • XmlDataSet
    • CompositeDataSet
    • StreamingDataSet
    • FilteredDataSet
    • DatabaseDataSet
    • XlsDataSet
    • QueryDataSet
    • ReplacementDataSet
  • DatabaseOperation
  • Operations (named "DatabaseOperation."xyz")
  •  
     
     
     
     
     
    • UPDATE
    • TRUNCATE 
    • INSERT
    • REFRESH
    • DELETE
    • CLEAN_INSERT
    • DELETE_ALL 
    • NONE
  • Other Operations
    • CompositeOperation
    • TransactionOperation
    • IdentityInsertOperation
Steps :

  1. Create your dataset load file
  2. Write your testcases
  3. Implement your testXXX() methods


Step 1: Create your dataset load file:
One unit test database instance can be created per developer with no data. DbUnit has the ability to export and import your database data to and from XML datasets. If the testcases are setup correctly, the data required for testing is loaded from the XML files before testcase execution and deleted after it is complete. So there will be no need to cleanup afterwards.
Data may also be generated from a number of sources:

  • flat XML files, of which we will see an example very shortly
  • tables in the database
  • database queries created using SQL
  • less obvious sources, such as Microsoft Excel spreadsheets
Here is a simple example of how to extract data into XML files from a Postgres database:


Step 2: Write your testcases

Create a wrapper class by extending the DBTestCase class. The wrapper class has the following important methods:

  • getDataSet (): you can use this to indicate how to load test data
  • getSetUpOperation() : defines the operation to be executed before the test is executed
  • getTearDownOperation() : defines clean up operation
To make testing easier, you can create a base class that extends DBTestCase for the entire project. It will look something like this:



Step 3. Implement your testXXX() methods
Implement your test methods using Junit. Your database is now initialized before and cleaned-up after each test methods according to what you did in previous steps.

By extending the base class created above, we can create any number of DBUnit testcases . Here is an example of a complete DBUnit testcase for testing code used for executing log queries using Spring framework. The methods tested here are getLogInDateRange(), addLogEntry() and updateLogEntry().




Advantages of DBUnit:

  • It is an excellent way to avoid the myriad of problems that can occur when one test case corrupts the database and causes subsequent tests to fail or exacerbate the damage
  • DbUnit can also help you to verify that your database data match an expected set of values and it has methods for comparing data, between flat files, queries and database tables
  • Because the test data remains unchanged in an XML file, testcases can be executed repeatedly after iterative code changes to verify if the code didn't break.

References:
http://www.dbunit.org/index.html

1 comments:

sarthak said...

The Stuff is good but the import statement should be there in the second example.