Java, Web and Mobile


From 0 to 100 – Test data for everyone

Java|Java EE|Testing

Imagine you’re new in a project and you want to start the project locally on your developer workspace. But unfortunately no useful data is available with which you can test some default cases. That’s awful.

Now imagine you are deploying the application and your database is prepared with predefined data. At each start – over and over again. Now I can see a developer smile.

In my previous posts to liquibase I have already recommended the use of h2 in memory database for local developer worspaces – this is our basis.

With the use of liquibase you are able to execute each SQL command. For example also simple inserts or updates etc. The “trick” is that I use the data only local and not on other stages as development or production. Now every developer has always a complete and fresh set of data. To do this just import an additional file in your db.changelog.xml. The additional file is therefore a change logfile which contains all the test data. But the test data will only be executed if the underlying database is a “h2 database” and “not in junit or test context”.

You can define the data synonymous in the main file but to keep the overview, I recommend a separate file only for test data.

Listing 1

Add <include file=”db.testdata.xml” /> at the end your existing db.changelog.xml

Listing 2

Create the new db.testdata.xml and add some test data

If you have activated the liquibase CDI producer, the application will start importing the data into your empty h2 database.

Listing 3

Log snippet


See also: