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

JIRA and Crucible

Recently I've started using some new tools from Atlassian and they have some really cool features compared to others I've used so far.

JIRA is Atlassian's issue tracking system and these are some of the things I like about it:
1. A cool web-based UI













2. Plug-ins for IntelliJ IDEA that makes it so much easier to use
















3. Workflow customizable to any project











4. A customizable dashboard with project reports and statistics













5. Multi-dimensional reporting - reports can be read in a variety of dimensions, including pie chart, linear or column graphs, and statistical tables












It was first launched in 2003 and so is relatively new compared to Bugzilla and other similar tools

Crucible is Atlassian's code review tool. It is a lightweight, web-based tool that makes code review a breeze.
For someone used to various code review tools, this seems to be the best and all developers will love it.

These are some of the cool things about Crucible:
1. The team doesn't have to meet together in one place. Code revies can be completed asynchronusly
2. Sub-tasks or issues can be created out of code review comments
3. JIRA integration
4. Inline comments makes it so much more easier to track the review and provide feedback
5. The diff tool is very useful and pleasant to use