SlideShare a Scribd company logo
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

What's hot

External Language Stored Procedures for MySQL
External Language Stored Procedures for MySQLExternal Language Stored Procedures for MySQL
External Language Stored Procedures for MySQL
Antony T Curtis
 
Perl Stored Procedures for MySQL (2009)
Perl Stored Procedures for MySQL (2009)Perl Stored Procedures for MySQL (2009)
Perl Stored Procedures for MySQL (2009)
Antony T Curtis
 

What's hot (20)

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

Similar to DbSetup

Oracle 12 c new-features
Oracle 12 c new-featuresOracle 12 c new-features
Oracle 12 c new-features
Navneet Upneja
 
SOUG_Deployment__Automation_DB
SOUG_Deployment__Automation_DBSOUG_Deployment__Automation_DB
SOUG_Deployment__Automation_DB
UniFabric
 
13 java beans
13 java beans13 java beans
13 java beans
snopteck
 

Similar to DbSetup (20)

Java Unit Testing with Unitils
Java Unit Testing with UnitilsJava Unit Testing with Unitils
Java Unit Testing with Unitils
 
PostgreSQL and PL/Java
PostgreSQL and PL/JavaPostgreSQL and PL/Java
PostgreSQL and PL/Java
 
Spring 4 final xtr_presentation
Spring 4 final xtr_presentationSpring 4 final xtr_presentation
Spring 4 final xtr_presentation
 
How To Build A Personal Portal On Google App Engine With Django
How To Build A Personal Portal On Google App Engine With DjangoHow To Build A Personal Portal On Google App Engine With Django
How To Build A Personal Portal On Google App Engine With Django
 
Polyglot persistence with Spring Data
Polyglot persistence with Spring DataPolyglot persistence with Spring Data
Polyglot persistence with Spring Data
 
Oracle 12 c new-features
Oracle 12 c new-featuresOracle 12 c new-features
Oracle 12 c new-features
 
Connect2016 AD1387 Integrate with XPages and Java
Connect2016 AD1387 Integrate with XPages and JavaConnect2016 AD1387 Integrate with XPages and Java
Connect2016 AD1387 Integrate with XPages and Java
 
AD1387: Outside The Box: Integrating with Non-Domino Apps using XPages and Ja...
AD1387: Outside The Box: Integrating with Non-Domino Apps using XPages and Ja...AD1387: Outside The Box: Integrating with Non-Domino Apps using XPages and Ja...
AD1387: Outside The Box: Integrating with Non-Domino Apps using XPages and Ja...
 
Ride the database in JUnit tests with Database Rider
Ride the database in JUnit tests with Database RiderRide the database in JUnit tests with Database Rider
Ride the database in JUnit tests with Database Rider
 
4 Essential Checklist to Manage Drupal Projects
4 Essential Checklist to Manage Drupal Projects4 Essential Checklist to Manage Drupal Projects
4 Essential Checklist to Manage Drupal Projects
 
Testing Django APIs
Testing Django APIsTesting Django APIs
Testing Django APIs
 
SOUG_Deployment__Automation_DB
SOUG_Deployment__Automation_DBSOUG_Deployment__Automation_DB
SOUG_Deployment__Automation_DB
 
Hadoop: Big Data Stacks validation w/ iTest How to tame the elephant?
Hadoop:  Big Data Stacks validation w/ iTest  How to tame the elephant?Hadoop:  Big Data Stacks validation w/ iTest  How to tame the elephant?
Hadoop: Big Data Stacks validation w/ iTest How to tame the elephant?
 
Google BigQuery 101 & What’s New
Google BigQuery 101 & What’s NewGoogle BigQuery 101 & What’s New
Google BigQuery 101 & What’s New
 
Apache DeltaSpike the CDI toolbox
Apache DeltaSpike the CDI toolboxApache DeltaSpike the CDI toolbox
Apache DeltaSpike the CDI toolbox
 
Apache DeltaSpike: The CDI Toolbox
Apache DeltaSpike: The CDI ToolboxApache DeltaSpike: The CDI Toolbox
Apache DeltaSpike: The CDI Toolbox
 
2022 COSCUP - Let's speed up your PostgreSQL services!.pptx
2022 COSCUP - Let's speed up your PostgreSQL services!.pptx2022 COSCUP - Let's speed up your PostgreSQL services!.pptx
2022 COSCUP - Let's speed up your PostgreSQL services!.pptx
 
13 java beans
13 java beans13 java beans
13 java beans
 
Ip project work test your knowledge
Ip project work test your knowledgeIp project work test your knowledge
Ip project work test your knowledge
 
In Memory Unit Testing with Apache DbUnit
In Memory Unit Testing with Apache DbUnitIn Memory Unit Testing with Apache DbUnit
In Memory Unit Testing with Apache DbUnit
 

More from fbenault (12)

Bdd java
Bdd javaBdd java
Bdd java
 
Property based-testing
Property based-testingProperty based-testing
Property based-testing
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
Test ng
Test ngTest ng
Test ng
 
Introduction to the language R
Introduction to the language RIntroduction to the language R
Introduction to the language R
 
Assertj-core
Assertj-coreAssertj-core
Assertj-core
 
Junit
JunitJunit
Junit
 
System rules
System rulesSystem rules
System rules
 
Db in-memory
Db in-memoryDb in-memory
Db in-memory
 
Guava
GuavaGuava
Guava
 
Java8
Java8Java8
Java8
 
Easymock
EasymockEasymock
Easymock
 

Recently uploaded

一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理
一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理
一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理
aagad
 
Article writing on excessive use of internet.pptx
Article writing on excessive use of internet.pptxArticle writing on excessive use of internet.pptx
Article writing on excessive use of internet.pptx
abhinandnam9997
 

Recently uploaded (12)

The+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptxThe+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptx
 
ER(Entity Relationship) Diagram for online shopping - TAE
ER(Entity Relationship) Diagram for online shopping - TAEER(Entity Relationship) Diagram for online shopping - TAE
ER(Entity Relationship) Diagram for online shopping - TAE
 
The Use of AI in Indonesia Election 2024: A Case Study
The Use of AI in Indonesia Election 2024: A Case StudyThe Use of AI in Indonesia Election 2024: A Case Study
The Use of AI in Indonesia Election 2024: A Case Study
 
How to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptxHow to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptx
 
Pvtaan Social media marketing proposal.pdf
Pvtaan Social media marketing proposal.pdfPvtaan Social media marketing proposal.pdf
Pvtaan Social media marketing proposal.pdf
 
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and GuidelinesMulti-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
 
一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理
一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理
一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理
 
How Do I Begin the Linksys Velop Setup Process?
How Do I Begin the Linksys Velop Setup Process?How Do I Begin the Linksys Velop Setup Process?
How Do I Begin the Linksys Velop Setup Process?
 
Article writing on excessive use of internet.pptx
Article writing on excessive use of internet.pptxArticle writing on excessive use of internet.pptx
Article writing on excessive use of internet.pptx
 
The Best AI Powered Software - Intellivid AI Studio
The Best AI Powered Software - Intellivid AI StudioThe Best AI Powered Software - Intellivid AI Studio
The Best AI Powered Software - Intellivid AI Studio
 
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shopHistory+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
 
The AI Powered Organization-Intro to AI-LAN.pdf
The AI Powered Organization-Intro to AI-LAN.pdfThe AI Powered Organization-Intro to AI-LAN.pdf
The AI Powered Organization-Intro to AI-LAN.pdf
 

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 ?