4
Most read
7
Most read
8
Most read
DBSetup
By Franck Benault
Created 03/07/2014
Last updated 13/08/2015
DBSetup plan
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
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)
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)
Advantages of DBSetup
● No XML
– Data are defined in Java
● Easy to factory the data set
● No dependencies
– DbUnit has 3 dependencies
● Fast
● OpenSource
Example1 : clean insert in a table
Operation DELETE_ALL = deleteAllFrom("USERS");
Operation INSERT_USERS_DATA =insertInto("USERS")
.columns("ID", "LOGIN", "PASSWORD")
.values(1L, "root", "pwd")
.values(2L, "guest", "pwd").build();
Operation operation = Operations.sequenceOf(
DBSetupCommonOperations.DELETE_ALL,
DBSetupCommonOperations.INSERT_USERS_DATA);
DbSetup dbSetup = new DbSetup(new DataSourceDestination(getDataSource()),
operation);
dbSetup.launch();
Example 2a: auto increment for the
keys
Operation DELETE_ALL = deleteAllFrom("USERS");
Operation INSERT_USERS_DATA = insertInto("USERS")
.withGeneratedValue("ID",
ValueGenerators.sequence().startingAt(100L).incrementingBy(10))
.columns("LOGIN", "PASSWORD")
.values("root", "pwd")
.values("guest", "pwd").build();
Operation operation = Operations.sequenceOf(
DBSetupCommonOperations.DELETE_ALL,
DBSetupCommonOperations.INSERT_USERS_DATA);
DbSetup dbSetup = new DbSetup(new DataSourceDestination(getDataSource()), operation);
dbSetup.launch();
Example 2b: repeating values
Operation DELETE_ALL = deleteAllFrom("USERS");
Operation INSERT_USERS_DATA_REPEATING = insertInto("USERS")
.withGeneratedValue("ID", ValueGenerators.sequence().startingAt(1L))
.withGeneratedValue("LOGIN",
ValueGenerators.stringSequence("user-").startingAt(1L))
.withGeneratedValue("PASSWORD",
ValueGenerators.stringSequence("pwd-").startingAt(1L))
.columns("DESCRIPTION").repeatingValues("fake description")
.times(10).build();
Operation operation = Operations.sequenceOf(
DBSetupCommonOperations.DELETE_ALL,
DBSetupCommonOperations.INSERT_USERS_DATA_REPEATING);
../..
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();
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
}
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"));
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);
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 ?

More Related Content

ODP
Unit testing: unitils & dbmaintain
ODP
Assertj-DB
PDF
Spring 4 - A&BP CC
PDF
spring3.2 java config Servler3
PDF
Getting Into Drupal 8 Configuration
ODP
Msql
PDF
Drupal 8 Services And Dependency Injection
PDF
Drupal 8 Services
Unit testing: unitils & dbmaintain
Assertj-DB
Spring 4 - A&BP CC
spring3.2 java config Servler3
Getting Into Drupal 8 Configuration
Msql
Drupal 8 Services And Dependency Injection
Drupal 8 Services

What's hot (20)

PDF
Building node.js applications with Database Jones
PPTX
New Authorization library for Joomla 4 (proposal)
PDF
My sql tutorial-oscon-2012
PDF
Upgrade your javascript to drupal 8
PDF
External Language Stored Procedures for MySQL
PPTX
Testing database content with DBUnit. My experience.
PDF
Czym jest webpack i dlaczego chcesz go używać?
PDF
Struts database access
PDF
An Introduction To PostgreSQL Triggers
PPTX
Drupal.js: Best Practices for Managing Javascript in Drupal
PPTX
Core Data Performance Guide Line
PPTX
Sqladria 2009 SRC
PDF
Utopia Kindgoms scaling case: From 4 to 50K users
PPT
Core Php Component Presentation
PDF
Perl Stored Procedures for MySQL (2009)
PDF
Developing for Node.JS with MySQL and NoSQL
PPT
Unit testing
PDF
Using Perl Stored Procedures for MariaDB
DOCX
Db examples
Building node.js applications with Database Jones
New Authorization library for Joomla 4 (proposal)
My sql tutorial-oscon-2012
Upgrade your javascript to drupal 8
External Language Stored Procedures for MySQL
Testing database content with DBUnit. My experience.
Czym jest webpack i dlaczego chcesz go używać?
Struts database access
An Introduction To PostgreSQL Triggers
Drupal.js: Best Practices for Managing Javascript in Drupal
Core Data Performance Guide Line
Sqladria 2009 SRC
Utopia Kindgoms scaling case: From 4 to 50K users
Core Php Component Presentation
Perl Stored Procedures for MySQL (2009)
Developing for Node.JS with MySQL and NoSQL
Unit testing
Using Perl Stored Procedures for MariaDB
Db examples
Ad

Similar to DbSetup (20)

PPTX
Java Unit Testing with Unitils
PDF
PostgreSQL and PL/Java
ODP
Spring 4 final xtr_presentation
PPTX
How To Build A Personal Portal On Google App Engine With Django
ODP
Polyglot persistence with Spring Data
PDF
Oracle 12 c new-features
PDF
AD1387: Outside The Box: Integrating with Non-Domino Apps using XPages and Ja...
PDF
Connect2016 AD1387 Integrate with XPages and Java
PDF
Best Practices for Effectively Running dbt in Airflow
PPTX
Ride the database in JUnit tests with Database Rider
PDF
4 Essential Checklist to Manage Drupal Projects
PPTX
Testing Django APIs
PPTX
SOUG_Deployment__Automation_DB
PDF
Hadoop: Big Data Stacks validation w/ iTest How to tame the elephant?
PPTX
Mobile Application Development (local database) class-07
PPTX
Google BigQuery 101 & What’s New
PDF
Apache DeltaSpike the CDI toolbox
PDF
Apache DeltaSpike: The CDI Toolbox
PPTX
2022 COSCUP - Let's speed up your PostgreSQL services!.pptx
PDF
13 java beans
Java Unit Testing with Unitils
PostgreSQL and PL/Java
Spring 4 final xtr_presentation
How To Build A Personal Portal On Google App Engine With Django
Polyglot persistence with Spring Data
Oracle 12 c new-features
AD1387: Outside The Box: Integrating with Non-Domino Apps using XPages and Ja...
Connect2016 AD1387 Integrate with XPages and Java
Best Practices for Effectively Running dbt in Airflow
Ride the database in JUnit tests with Database Rider
4 Essential Checklist to Manage Drupal Projects
Testing Django APIs
SOUG_Deployment__Automation_DB
Hadoop: Big Data Stacks validation w/ iTest How to tame the elephant?
Mobile Application Development (local database) class-07
Google BigQuery 101 & What’s New
Apache DeltaSpike the CDI toolbox
Apache DeltaSpike: The CDI Toolbox
2022 COSCUP - Let's speed up your PostgreSQL services!.pptx
13 java beans
Ad

More from fbenault (12)

ODP
Bdd java
ODP
Property based-testing
ODP
Java concurrency
ODP
Test ng
ODP
Introduction to the language R
ODP
Assertj-core
ODP
Junit
ODP
System rules
ODP
Db in-memory
ODP
Guava
ODP
Java8
ODP
Easymock
Bdd java
Property based-testing
Java concurrency
Test ng
Introduction to the language R
Assertj-core
Junit
System rules
Db in-memory
Guava
Java8
Easymock

Recently uploaded (20)

PPTX
Tìm hiểu về dịch vụ FTTH - Fiber Optic Access Node
PDF
The Evolution of Traditional to New Media .pdf
PPTX
Artificial_Intelligence_Basics use in our daily life
PPTX
Internet Safety for Seniors presentation
PDF
The_Decisive_Battle_of_Yarmuk,battle of yarmuk
PDF
Buy Cash App Verified Accounts Instantly – Secure Crypto Deal.pdf
PDF
KEY COB2 UNIT 1: The Business of businessĐH KInh tế TP.HCM
PDF
Course Overview and Agenda cloud security
PDF
Computer Networking, Internet, Casting in Network
PPSX
AI AppSec Threats and Defenses 20250822.ppsx
PPTX
10.2981-wlb.2004.021Figurewlb3bf00068fig0001.pptx
PDF
simpleintnettestmetiaerl for the simple testint
PPTX
Top Website Bugs That Hurt User Experience – And How Expert Web Design Fixes
PPT
12 Things That Make People Trust a Website Instantly
PPTX
The-Importance-of-School-Sanitation.pptx
PPTX
Partner to Customer - Sales Presentation_V23.01.pptx
PPTX
module 1-Part 1.pptxdddddddddddddddddddddddddddddddddddd
PPTX
在线订购名古屋艺术大学毕业证, buy NUA diploma学历认证失败怎么办
PPTX
KSS ON CYBERSECURITY INCIDENT RESPONSE AND PLANNING MANAGEMENT.pptx
PDF
mera desh ae watn.(a source of motivation and patriotism to the youth of the ...
Tìm hiểu về dịch vụ FTTH - Fiber Optic Access Node
The Evolution of Traditional to New Media .pdf
Artificial_Intelligence_Basics use in our daily life
Internet Safety for Seniors presentation
The_Decisive_Battle_of_Yarmuk,battle of yarmuk
Buy Cash App Verified Accounts Instantly – Secure Crypto Deal.pdf
KEY COB2 UNIT 1: The Business of businessĐH KInh tế TP.HCM
Course Overview and Agenda cloud security
Computer Networking, Internet, Casting in Network
AI AppSec Threats and Defenses 20250822.ppsx
10.2981-wlb.2004.021Figurewlb3bf00068fig0001.pptx
simpleintnettestmetiaerl for the simple testint
Top Website Bugs That Hurt User Experience – And How Expert Web Design Fixes
12 Things That Make People Trust a Website Instantly
The-Importance-of-School-Sanitation.pptx
Partner to Customer - Sales Presentation_V23.01.pptx
module 1-Part 1.pptxdddddddddddddddddddddddddddddddddddd
在线订购名古屋艺术大学毕业证, buy NUA diploma学历认证失败怎么办
KSS ON CYBERSECURITY INCIDENT RESPONSE AND PLANNING MANAGEMENT.pptx
mera desh ae watn.(a source of motivation and patriotism to the youth of the ...

DbSetup

  • 1. DBSetup By Franck Benault Created 03/07/2014 Last updated 13/08/2015
  • 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
  • 7. Example1 : clean insert in a table Operation DELETE_ALL = deleteAllFrom("USERS"); Operation INSERT_USERS_DATA =insertInto("USERS") .columns("ID", "LOGIN", "PASSWORD") .values(1L, "root", "pwd") .values(2L, "guest", "pwd").build(); Operation operation = Operations.sequenceOf( DBSetupCommonOperations.DELETE_ALL, DBSetupCommonOperations.INSERT_USERS_DATA); DbSetup dbSetup = new DbSetup(new DataSourceDestination(getDataSource()), operation); dbSetup.launch();
  • 8. Example 2a: auto increment for the keys Operation DELETE_ALL = deleteAllFrom("USERS"); Operation INSERT_USERS_DATA = insertInto("USERS") .withGeneratedValue("ID", ValueGenerators.sequence().startingAt(100L).incrementingBy(10)) .columns("LOGIN", "PASSWORD") .values("root", "pwd") .values("guest", "pwd").build(); Operation operation = Operations.sequenceOf( DBSetupCommonOperations.DELETE_ALL, DBSetupCommonOperations.INSERT_USERS_DATA); DbSetup dbSetup = new DbSetup(new DataSourceDestination(getDataSource()), operation); dbSetup.launch();
  • 9. Example 2b: repeating values Operation DELETE_ALL = deleteAllFrom("USERS"); Operation INSERT_USERS_DATA_REPEATING = insertInto("USERS") .withGeneratedValue("ID", ValueGenerators.sequence().startingAt(1L)) .withGeneratedValue("LOGIN", ValueGenerators.stringSequence("user-").startingAt(1L)) .withGeneratedValue("PASSWORD", ValueGenerators.stringSequence("pwd-").startingAt(1L)) .columns("DESCRIPTION").repeatingValues("fake description") .times(10).build(); Operation operation = Operations.sequenceOf( DBSetupCommonOperations.DELETE_ALL, DBSetupCommonOperations.INSERT_USERS_DATA_REPEATING); ../..
  • 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 ?