Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

DbUnit Express Tips: Setup Simplification, Custom Data File Convention

DZone's Guide to

DbUnit Express Tips: Setup Simplification, Custom Data File Convention

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

I’ve recently introduced here DbUnit Express, a wrapper around DbUnit intended to get you started with testing DB-related code in no time, and now I’d like to share two productivity tips: simplifying db tester setup with a parent test and implementing your own convention for data set files, for example one data set per test class.

Define your own convention for data set names, e.g. per-test-class-data-set

By default DbUnit Express expects you to use dataset testData/dbunit-test_data_set.xml. However you might for example prefer each test to have its own data set, named for example <test class name>-test.xml.

The easiest solution without repeating yourself is to create a custom subclass of EmbeddedDbTester overriding its createDefaultDataSet():

public class PerTestDataSetEmbeddedDbTester extends EmbeddedDbTester {
    @Override
    protected IDataSet createDefaultDataSet() throws DatabaseUnitRuntimeException, DataSetException {
		return createDataSetFromFile(getClass().getSimpleName() + "-data.xml");
	}
}

Notice that if the data set cannot be found in the default location, i.e. testData/, then it is searched for on the classpath, so it is perfectly OK to have it next to the test class.

Setup Simplification

When using DbUnit Express with JUnit 4.x, you typically need to do three things in each test class:

public class SpringBookDaoImplTest {

    private EmbeddedDbTester testDb = new EmbeddedDbTester(); // 1

    @Before
    public void setUp() throws Exception {
        testDb.onSetup(); // 2
    }

    @After
    public void tearDown() throws Exception {
        testDb.onTearDown(); // 3
    }

    ...
}

That is lot of repeated coding and the best way to get rid of it is to create an abstract parent class of all your test classes working with DbUnit Express and moving the code there (changing the tester’s visibility to protected, of course):

public abstract class AbstractMyApplicationDbTest {

    protected EmbeddedDbTester testDb = new EmbeddedDbTester(); // 1

    @Before
    public final void setUpDbUnitExpress() throws Exception {
        testDb.onSetup(); // 2
    }

    @After
    public final void tearDownDbUnitExpress() throws Exception {
        testDb.onTearDown(); // 3
    }

    ...
}

 

From http://theholyjava.wordpress.com/2011/08/05/dbunit-express-tips-setup-simplificatio-custom-data-file-convention/

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}