Dynamically Creating Data Sets

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Dynamically Creating Data Sets

Marc Philipp-2
Hi all,

first of all, let me say that I've found DBUnit to be incredibly
useful. Nevertheless, there was one thing that always disturbed me:
I've found understanding tests using DBUnit often hard because I had
to switch back and forth between multiple files, i.e. the actual test
code and the XML data set.

Over time an idea was born: Wouldn't it be possible to leverage the
power of DBUnit but create the data set right in the test code?

After researching and looking through the DBUnit code -- especially
FlatXmlDataSetBuilder and the classes it uses -- for a while, I
figured it possible but there was no nice API to do it, yet.
Therefore, I came up with a class called DataSetBuilder which is
basically a wrapper around a CachedDataSet using a BufferedConsumer.

Let's take a look at an example:

    DataSetBuilder builder = new DataSetBuilder();

    // Using strings as column names, not type-safe
    builder.newRow("PERSON").with("NAME", "Bob").with("AGE", 18).add();

    // Using ColumnSpecs to identify columns, type-safe!
    ColumnSpec<String> name = ColumnSpec.newColumn("NAME")
    ColumnSpec<Integer> age = ColumnSpec.newColumn("AGE");
    builder.newRow("PERSON").with(name, "Alice").with(age, 23).add();

    // New columns are added on the fly
    builder.newRow("PERSON").with(NAME, "Charlie").with("LAST_NAME",

    IDataSet dataSet = builder.build();

The code listed above creates three records in the PERSON table. It
showcases two different ways of specifying columns, one using plain
Strings and one using ColumnSpec instances, respectively.

You might now print it out to the console or a file, i.e.

    new FlatXmlWriter(new PrintWriter(System.out)).write(dataSet);

will print

      <PERSON NAME="Bob" AGE="18"/>
      <PERSON NAME="Alice" AGE="23"/>
      <PERSON NAME="Charlie" LAST_NAME="Brown"/>

I think I found a way to create a data set directly from Java code in
a readable way. In addition, creating the data sets programmatically
gives to tools like refactoring, search for references, and so on for

Now, I'm very interested in hearing your opinions about this.
Therefore, I pushed the code to a public GitHub repository for easy
access. However, in the end, I would be more than glad if you would
consider this an addition to DBUnit and incorporate it directly into
your codebase.

Here is the Git repository: https://github.com/marcphilipp/dbunit-datasetbuilder

So, what do you think?

Best regards,


Enable your software for Intel(R) Active Management Technology to meet the
growing manageability and security demands of your customers. Businesses
are taking advantage of Intel(R) vPro (TM) technology - will your software
be a part of the solution? Download the Intel(R) Manageability Checker
today! http://p.sf.net/sfu/intel-dev2devmar
dbunit-user mailing list
[hidden email]