DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Having Database Tests Ready in 10 Minutes with DbUnit Express

Having Database Tests Ready in 10 Minutes with DbUnit Express

Jakub Holý user avatar by
Jakub Holý
·
Jul. 28, 11 · Java Zone · Interview
Like (0)
Save
Tweet
8.15K Views

Join the DZone community and get the full member experience.

Join For Free

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>
 	<groupId>net.jakubholy.testing</groupId>
	<artifactId>dbunit-embeddedderby-parenttest</artifactId>
	<version>1.2.0</version>
	<scope>test</scope>
</dependency>

<dependency> <!-- won't be required in future versions -->
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-simple</artifactId>
	<version>1.5.6</version>
	<scope>test</scope>
</dependency>
  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/

Database Testing Express

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 3 Pieces of Bad Advice on How to Keep Your IT Job
  • Take Control of Your Application Security
  • Top 11 Cloud Platforms for Internet of Things (IoT)
  • Implementing RBAC Configuration for Kubernetes Applications

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo