Over a million developers have joined DZone.

Having Database Tests Ready in 10 Minutes with DbUnit Express

DZone's Guide to

Having Database Tests Ready in 10 Minutes with DbUnit Express

· Java Zone
Free Resource

Managing a MongoDB deployment? Take a load off and live migrate to MongoDB Atlas, the official automated service, with little to no downtime.

DbUnit Express is my wrapper around DbUnit that intends to make it extremely easy to set up a test of a code that interacts with a database. It is preconfigured to use an embedded Derby database (a.k.a. JavaDB, part of SDK) and uses convention over configuration to find the test data. It can also with one call create the test DB from a .ddl file. Aside of simplifying the setup it contains few utilities to make testing easier such as getDataSource() (essential for testing Spring JDBC) and RowComparator.

Originally I was using DbUnit directly but I found out that for every project I was copying lot of code and thus I decided to extract it into a reusable project. Since that it has further grown to be more flexible and make testing even easier.

Here are the seven easy steps to have a running database test:

  1. Add binaries: Add dbunit-embeddedderby-parenttest 1.2.0 to your dependencies (note: in future versions it will be renamed to dbunit-express)

<dependency> <!-- won't be required in future versions -->
  1. Create the folder testData/ under your project’s root folder.
  2. (optional) Prepare .ddl: Copy the example testData/create_db_content.ddl into the new testData/ and modify it to fit your data schema needs
  3. (optional) Create & initialize DB: Run DatabaseCreator#main from your project’s root folder to create and initialize the test DB from the .ddl file.
  4. Write a TestCase: Either subclass AbstractEmbeddedDbTestCase or create and use an instance of EmbeddedDbTester in your test class and implement your tests there. (See SimpleEmbeddedDbTest.java and SimpleNonExtendingEmbeddedDbTest.java for inspiration.) You may want to override its getDataSet() if you want to use st. else than the default testData/dbunit-test_data_set.xml.
  5. Prepare test data: Copy the example testData/dbunit-test_data_set.xml into your new testData/ and modify it to fit your data needs. Any table mentioned in the file will be emptied prior to running test, if it has any rows defined than those will be also inserted
    • You may use e.g. Jailer (good tutorial) to produce a subset of your database in DbUnit XML format (unfortunately only Flat XML) or another tool supporting it such as QuantumDB.
  6. Run the test.

You may read more about DbUnit Express on its home page.

PS: The last released version 1.2.0 is from 4/2010, a new version should be out in few weeks.


From http://theholyjava.wordpress.com/2011/07/27/having-database-test-ready-in-10-minutes-with-dbunit-express/

MongoDB Atlas is the easiest way to run the fastest-growing database for modern applications — no installation, setup, or configuration required. Easily live migrate an existing workload or start with 512MB of storage for free.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}