• Home
  • RSS Feed
  • Log in


Sharpen up your DbUnit testing
Posted by Saket Vishal just before lunchtime: September 21st, 2007

DbUnit is a JUnit extension and is very popular among developers for unit testing database-driven projects. In this blog I will discuss some of the tips to make your life easier and beautiful for DbUnit testing.

Using same connection for entire test suite
If you are extending DatabaseTestCase then this class creates a connection to the database in setUp() and closes it in teardown() this can prove to be a big overhead and can cause considerable slowdown in running tests as the number of test cases increase. Maintaining a single connection for the entire test suite can prove to be beneficial in such cases.

If you are using JUnit 4 then you can use annotations for performing initial operations (@BeforeClass) and thus there is no need to extend the DatabaseTestCase and finally handling the closing of database connection.

@BeforeClass
public static void setUpBeforeClass() throws Exception {
handleSetUpOperation();
}

If several test cases are using a read only dataset as expected state of the database after running the test cases then loading this expected dataset file only once for all the test cases can help up in speeding up things.

Specifying a DTD for the dataset can act as a cure for some of the things
You can create a DTD by using the following code in DbUnit.

FlatDtdDataSet.write(connection.createDataSet(),
new FileOutputStream("test.dtd")); 

This DTD can be used now to
- Validate a dataset file against it.
- Providing null values to a column by omitting it for a row in dataset.

Keeping some reusable methods
A few reusable methods like asserting the database state after running tests with an expected state dataset file can be very useful. I am mentioning one of such methods here

    protected void assertTables(String filename, String[] tableNames)
    throws SQLException, Exception, DataSetException, IOException,
    DatabaseUnitException {
    IDataSet expectedDataSet = null;
    if (tableNames == null) {
    // Load expected data from an XML data set
    expectedDataSet = new FlatXmlDataSet(new File(filename));
    tableNames = expectedDataSet.getTableNames();
    } else {
    // Load subset of the complete dataset
    expectedDataSet = new FilteredDataSet(tableNames,
    new FlatXmlDataSet(new File(filename)));
    }
    // Fetch database data after executing your code
    IDataSet actualDataSet = getConnection().createDataSet(tableNames);
    ITable[] expectedTables = new CompositeTable[tableNames.length];
    for (int i = 0; i <tableNames.length; i++) {
    expectedTables[i] = new CompositeTable(expectedDataSet
    .getTableMetaData(tableNames[i]), actualDataSet
    .getTable(tableNames[i]));
    }
    Assertion.assertEquals(expectedDataSet, new CompositeDataSet(
    expectedTables));
    }

Above method assertTables can assert the current state of the database after running test cases with the state provided in the dataset file. This method is intelligent enough to handle both cases of asserting the database state with the expected dataset file by supplying table names (might be a subset of all the tables provided in the dataset file) or without the table names in which case all the tables provided in the dataset files are used. The method uses table meta-data provided in the dataset file to get state of the corresponding table from the database, thus to ignore column such as auto-generated column can be automatically avoided in comparison by not mentioning them in the dataset file.

Happy Red-Green-Refactor-ing.

Share

Filed under Java | 3 Comments »



3 Responses to “Sharpen up your DbUnit testing”



    Andres Almiray Says:
    Posted at: September 21, 2007 at 5:34 pm

    I’m curious to know how you implemented handleSetUpOperation() without extending DatabaseTestCase (or DBTestCase). As a matter of fact that scenario is one of the reasons why DbUnit 2.2 includes IDatabaseTester =)

    Reply


    DbUnit : Testing with Database « Elope Says:
    Posted at: October 5, 2007 at 5:39 am

    [...] Blog about DbUnit:Sharpen up your DbUnit testing [...]

    Reply


    Sandy Says:
    Posted at: January 5, 2011 at 8:33 am

    May i know that how can i use DBUnit (with junit4) without extending DBtestcase/DataBasetestcase

    Reply


Leave a Reply

Click here to cancel reply.


Xebia Sites

  • Xebia Corporate
  • Xebia France
  • Xebia India
  • XebiCon 2012

Categories

  • Java (312)
  • Agile (192)
  • General (141)
  • Scrum (70)
  • Testing (65)
  • Architecture (65)
  • Performance (47)
  • Middleware (59)
    • Deployment (40)
  • Xebia Labs (41)
  • SOA (31)
  • Project Management (31)
  • Podcast (31)
  • Tools (28)
  • Uncategorized (24)
  • lean architecture (20)
  • Quality Assurance (19)
  • Articles (15)
  • Requirements Management (14)
  • Virtualization (21)

Tag Cloud

    Concurrency Control Architecture Javascript Scrum JPA TDD Groovy JPA implementation patterns Lean Eclipse lean architectuur Java Maven Flex SOA ACT Grails agile architectuur lean architecture Hibernate product owner Scala Frameworks Spring Ajax XML Moving to India Oracle Xebia Agile

Archives

  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
Avatars by Sterling Adventures