Week 9 - DBSetup
19 Jul 2014In the following blog post I want to introduce you to DBSetup. It is an alternative to DBUnit that is OpenMRS’ standard database tool for unit tests.
DBUnit
For those of you who don’t know DBUnit I will give you a short example: DBUnit uses a flat xml file, where all data is defined
This file creates two new locations and assigns the location tag “Operation Theater” to them. Perhaps you have mentioned fields like “creator”, “date_created”, “uuid” and “retired” that have to be defined. We come back to that later.
These are the disadvantages of DBUnit
- DBUnit forces you to specify every column that is not nullable and doesn’t have a default value defined in the db schema
- A best practice is to use one file for one test. This means if a column is added/removed one has to edit all dbunit files!
- XML is verbose
- Data definition and test case are in seperate files.
DBSetup
Due to this shortcomings I was curious if there is an alternative approach out there. I launched my search engine and stackoverflow and found other people with the same desire. Shortly afterwards, I came across DBSetup from ninja-squad. It looked very promising and I decided to give it a shot.
The main difference to DBUnit is that there is no xml file. Data that should be inserted is defined with Java code
Here is a short example from their website:
You see that you don’t have to repeat the column name for each row that you want to insert - that’s nice, but not the killer feature right? But if we look more closely at this example we notice line two:
This means we can omit the version field from our inserts. Its older brother
is even more powerful. With his help we can define sequences of integers, strings and dates out of the box. This is the feature that I love most in DBSetup! In the example above we could even remove the “ID” field by declaring
This would lead to the same result as the default sequence starts at 1 and is incremented by 1.
"creator", "date_created", ...
Now it’s time to come back to the fields (“creator”, “date_created”, “uuid” and “retired”). During testing, usually I just don’t care about this values. With the help of value generators and a utility function, inserting two locations has become a lot easier:
All other values including the primary key and uuid (“Location1”, “Location2”) are inserted automatically.
The magic happens inside the utility class DbUtil and its inner class Config. You can find the internals in this commit
OpenMRS integration
Finally I want to give you an example of how it looks like inside an actual OpenMRS test class
Summary
In this blog post I have presented an alternative libary to DbUnit and how it can be integrated into OpenMRS. Value generators are a great way to automatically populate row columns. Data is defined with java code rather than as xml file which is more powerful. If you add a new field to a table you don’t have to rewrite all your tests. The only thing you have to do is to add another default value generator to the config inside DbUtil class.