Most database testing tools use XML, YAML or SQL files to define test data sets. And those approaches have several drawbacks:
<dataset> <PERSON ID="1" FIRST_NAME="John" LAST_NAME="Doe" BIRTH_DATE="1975-05-06" VERSION="0"/> <PERSON ID="2" FIRST_NAME="Mark" LAST_NAME="Smith" BIRTH_DATE="1980-07-03" VERSION="0"/> <PERSON ID="3" FIRST_NAME="Claire" LAST_NAME="Connell" BIRTH_DATE="1981-09-17" VERSION="0"/> </dataset>
Insert.into("PERSON") .withDefaultValue("VERSION", 0) .columns("ID", "FIRST_NAME", "LAST_NAME", "BIRTH_DATE") .values(1, "John", "Doe", "1975-05-06") .values(2, "Mark", "Smith", "1980-07-03") .values(3, "Claire", "Connell", "1981-09-17") .build();
DbSetup doesn't try to do everything for you and to guess which tables should be deleted based
on what has been inserted, and in which order. You're not limited to deletes and inserts. If you have
some cyclic dependencies in your data set, you can use an update statement, or even disable constraints,
wherever you want during the setup, using a SqlOperation
.
Defining your own operations is easy.
Since everything is done and defined in Java, inserting a complex piece of data, or a large number of rows, or a piece of binary data that can't be written easily in XML, is not a problem.
Reusing a common part of a setup, parameterize it, is as simple as defining and calling a Java method.
Most of the database operations in an application, especially when you use an ORM, are read-only operations.
A typical repository contains many find()
methods that you want to test, and very few create()
or save()
. Instead of re-populating the database before each test, even though the previous one
used the same data set and was read-only,
DbSetup allows populating the database only if it needs to be populated. By using a DbSetupTracker
,
and adding a single line of code in your read-only tests, you can avoid lots of unnecessary database repopulations,
making your tests run much faster, and still leaving test methods independent.
DbSetup contains very few classes, and typical usage involves a very small portion of these classes. You should be up and running in a few minutes.