3. DBSetup : introduction
● I have used DBUnit for several years in Java EE project
including access to Dabase (often with Hibernate)
● I have a lot of Junit tests with DBUnit
● DBUnit has helpt me a lot... but I am not satisfy by the
result
● The test are heavy, slow, difficult to maintain
● The solution is new library DBSetup
4. Issues with DBUnit
● DBUnit is not independant, this is a Junit extension
● DBUnit is using XML files (verbious, far from Java code)
● DBUnit is larger than DbSetup
– DBSetup : data injection in database only
– DBUnit : it is possible to check the content of the
database (not useful)
5. Presentation of DBSetup
● Goal : setup your database to execute unit tests
– Like DbUnit but simplier
● Web site :
● http://dbsetup.ninja-squad.com/
● Last version 1.6.0 (06/2015)
6. Advantages of DBSetup
● No XML
– Data are defined in Java
● Easy to factory the data set
● No dependencies
– DbUnit has 3 dependencies
● Fast
● OpenSource
10. Example 3: date
● I want in my dataset a date = yesterday or tomorrow or
one year ago...
● Very difficult with DBUnit
● Very easy DbSetup because the dataset is defined in the
java code
Operation INSERT_USERS_DATA = insertInto("USERS")
.columns("ID","LOGIN", "PASSWORD", "DEACTIVATION_DATE")
.values(1L,"root", "pwd", DateUtil.getTomorrow())
.values(2L,"guest", "pwd", DateUtil.getTomorrow())
.values(3L,"guest", "pwd", DateUtil.getYesterday()).build();
11. Example 4: setup tracker
● My data set may be modified by the tests
@Before
public void setUp() {
.../...
DbSetup dbSetup = new DbSetup(new DataSourceDestination(dbManager.getDataSource()), operation);
dbSetupTracker.launchIfNecessary(dbSetup);
}
@Test
public void testFindAllUsers() {
dbSetupTracker.skipNextLaunch();
// a test which does not modify the data set
}
12. Example5 : cyclic dependencies
● How to update a (bad designed) data model when two tables a
linked with foreign
● With DBUnit no solution / DbSetup add sql request...
Operation insertVendorsAndProducts = sequenceOf(
insertInto("VENDOR")
.columns("ID", "VCODE", "NAME")
.values(1L, "AMA", "AMAZON")
.build(),
insertInto("PRODUCT")
.columns("ID", "NAME", "VENDOR_ID")
.values(1L, "Kindle", 1L).build(),
sql("update VENDOR set FEATURED_PRODUCT_ID = 1 where ID = 1"));
13. Example 6 : DBUnit compare
tables
● DBSetup is limited to fill database
● With DBUnit it is possible to compare the content of the database
with an excepted result defined in XML
// Fetch database data after executing your code
IDatabaseConnection dc = new DatabaseConnection(dbManager.getConnection());
IDataSet databaseDataSet = dc.createDataSet();
ITable actualTable = databaseDataSet.getTable("USERS");
// Load expected data from an XML dataset
InputStream is = UserQueriesTestWithDbUnit.class.getResourceAsStream("/usersWithoutGuest.xml");
IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(is);
ITable expectedTable = expectedDataSet.getTable("USERS");
// Assert actual database table match expected table
new DbUnitAssert().assertEquals(expectedTable, actualTable);
14. Conclusion
● DBSetup as fast or a little faster as DBUnit
● Ready to use DBSetup ?
● Questions
– How to migrate from DBUnit to DBSetup
● Tools XML -> Java
– Futur of DBSetup ?