a peek into my MIND

August 16, 2010

DbUnit and setting up changing data.

Filed under: Java, Testing — Tags: , , , — Bharat Kondeti @ 10:52 pm

DbUnit is used to set up test data for integration tests that rely on a database. One way of importing and exporting database data in DbUnit is by XML data-sets. Assuming we have a table like this…

CUSTOMER
—————————————————————————–
| ID | FirstName | LastName | LicenceExpiryDate |
—————————————————————————–

We are testing a method called isLicenceExpired(id). To unit test this method we can setup some dummy data using DbUnits.

<?xml version='1.0' encoding='UTF-8'?>
<dataset>
  <CUSTOMER ID="1" FIRSTNAME="FirstName1" LASTNAME="LastName1" LICENCEEXPIRTDATE="2010-08-25" 
  <CUSTOMER ID="2" FIRSTNAME="FirstName2" LASTNAME="LastName2" LICENCEEXPIRTDATE="2010-09-15" 
  <CUSTOMER ID="3" FIRSTNAME="FirstName3" LASTNAME="LastName3" LICENCEEXPIRTDATE="2011-01-01" 
</dataset>

Problem with above setup is test-cases would eventually fail because of ‘LicenceExpiryDate’. We need to setup this date such that it’s relative to current date. This is where ReplacementDataSet comes into picture. This class decorates FlatXmlDataSet class to provide replacement functionality. Now we can define your data set as

<?xml version='1.0' encoding='UTF-8'?>
<dataset>
  <CUSTOMER ID="1" FIRSTNAME="FirstName1" LASTNAME="LastName1" LICENCEEXPIRTDATE="${today+10}" 
  <CUSTOMER ID="2" FIRSTNAME="FirstName2" LASTNAME="LastName2" LICENCEEXPIRTDATE="${today+30}" 
  <CUSTOMER ID="3" FIRSTNAME="FirstName3" LASTNAME="LastName3" LICENCEEXPIRTDATE="${today+100}" 
</dataset>

and example setupDataWithReplacement method would look like

public static void setupDataWithReplacement(String fileName, Map<String,Object> replacements) throws Exception {
  
  //Get database connection.
  Connection conn = ds.getConnection();
  //When there are multiple databases 
  conn.setCatalog(DATABASE_NAME);
  try {
    IDatabaseConnection connection = new DatabaseConnection(conn,DATABASE_NAME);
    connection.getConfig().setProperty(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);

    IDataSet dataSet = new FlatXmlDataSet(AbstractTransactionalDasDataTests.class.getClassLoader().getResourceAsStream(fileName));

    //Decorate the class and call addReplacementObject method
    ReplacementDataSet rDataSet = new ReplacementDataSet(dataSet);
    for (String key : replacements.keySet()) {
      rDataSet.addReplacementObject("${"+key+"}", replacements.get(key));
    }

    DatabaseOperation.INSERT.execute(connection, rDataSet);
			
  } catch (Exception e) {
    e.printStackTrace();
  } finally {
    DataSourceUtils.releaseConnection(conn, ds);
  }
}

Now we can setup data like before we run our tests

Date today = new Date();
Map<String,Object> replacements = new HashMap<String,Object>();
replacements.put("today+10", DateUtils.addDays(today, 10));
replacements.put("today+30", DateUtils.addDays(today, 30));
replacements.put("today+100", DateUtils.addDays(today, 100));
setupDataWithReplacement("customer-test-data.xml",replacements);
Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: