Data Sets - put your database in an predefined state

The concepts of InitialDataSets provide a convenient way to populate your test database (default the in memory database) with a predefined set of data. This means every test can rely on a well defined set of data, loaded into the database, every time a test is executed.

Ejb3Unit comes with two different InitialDataSet implementations:

  • EntityInitialDataSet - provides a way to programatically create instances of persistent objects. This objects are automatically persistent before every test execution and after the test completes this data is deleted again.
  • CSVInitialDataSet - provides a way to populate your test data base with larger amount of data. The data is loaded from a comma separated value files (CSV) and stored in the database before every test run. CSVInitialDataSet supports also relational data (e.g.a Order can be loaded with the LineItems of this Order).

EntityInitialDataSet

The following example shows how sets of persistent objects can be created programatically. You need to extend the base class EntityInitialDataSet:

public static class OrderInitialDataSet extends
                        EntityInitialDataSet<Order> {

                public StockWKNEntityInitialDataSet() {
                        super(Order.class);
                }

                public void create() {
                        this.add(new Order(1, "First Order"));
                        this.add(new Order(2, "Second Order"));

                }

An instance of "OrderInitialDataSet" can be passed to a SessionBeanFixture or a } POJO fixture to prepopulate every test with this Order data.

CSVInitialDataSet

CSV files provides convinient way to bulk load data into your database. CSVInitialDataSet supports comma separated files. If the special character comma is used indide a column quotes shoud be used

In this case a line wold look like: "500340","ADIDAS SALOMON AG O."," DE0005003404"

If you want to use CSVInitialDataSet a saple code look like this:

  private static final CSVInitialDataSet<StockWKNBo> CSV_SET = 
                new CSVInitialDataSet<StockWKNBo>(
                        StockWKNBo.class, "allStocks.csv", "wkn", "isin", "symbol");