Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Java Unit Testing with Unitils


Published on

Presentation from JEEConf conference (Kiev, May 2011) about unit testing in Java with Unitils library.

Published in: Technology
  • Be the first to comment

Java Unit Testing with Unitils

  1. 1. Java Unit Testing with Unitils<br />Mikalai Alimenkou<br /><br />21.05.2011<br />
  2. 2. Background<br />Java Technical Lead/Scrum Master at Zoral Labs<br />6+ years in software development<br />4+ years of working by Agile methodologies<br />Expert in Agile engineering practices<br />Founder and coachat XP Injection (<br />Organizer of Selenium Camp and JEEConf conferences <br />
  3. 3. General Idea<br />TestNG<br />
  4. 4. Unitils History<br />Spring test integration<br />Multi db support<br />Various improvements<br />Unitils 3.0<br />DbMaintain 2.2<br />Testing guidelines<br />from brainstorm<br />Separate modules<br />Unitils 2.0<br />Unitils 1.0<br />DbMaintain 2.0<br />DbMaintain separated <br />from Unitils<br />Unitils RC1<br />
  5. 5. Unitils Facts<br />Open source<br />Integrates with JUnit and TestNG<br />Modular and flexible architecture<br />Lots of useful utilities<br />
  6. 6. Modular Architecture<br />Existing modules<br />DatabaseModule<br />DbUnitModule<br />HibernateModule<br />MockModule<br />EasyMockModule<br />InjectModule<br />SpringModule<br />Create new or change implementation<br />Configure only needed modules<br />
  7. 7. Flexible Configuration<br />Everything is configurable<br />3 levels of overridden properties<br />
  8. 8. Database Testing<br />Main problems<br />Mocking or real data?<br />JDBC, Hibernate, JPA, …<br />Testing approaches<br />Create database, clean database<br />Local transactions<br />
  9. 9. Unitils Solution<br />Database maintainer<br />Small test datasets with DbUnit<br />@Transactional support<br />Separate schema per developer<br />Custom DataSource for more control<br />
  10. 10. Database Maintenance<br />Keep database schema in VCS<br />Great versioning technique with incremental updates<br />Ability to disable constraints<br />
  11. 11. Database Maintenance<br />Supports large number of databases<br />Prepares sequences and identity columns<br />Many options for database and data maintenance<br />Generate XSD or DTD for DbUnit datasets<br />
  12. 12. DbMaintain Project<br /><br />Separated from Unitils<br />Can be used to automate deployment and upgrade procedure<br />Version 2.3 in May 2011<br />
  13. 13. DbMaintain Scripts Structure<br />Incremental database updates<br />Committed files are unchangeable<br />Strict sequence<br />Supports repeatable scripts, postprocessing<br />
  14. 14. DbMaintain Scripts Structure<br />Rename of scripts and folders is supported<br />Multi-database and patches support<br />Script information is stored in special table<br />
  15. 15. DbMaintain Execution<br />Ant<br /><updateDatabasescriptLocations="${database.archive}"> <br /> <database driverClassName="org.hsqldb.jdbcDriver" url="jdbc:hsqldb:mem:mydb" userName="admin" password="pwd" schemaNames="PUBLIC" /> <br /> </updateDatabase><br />Command line<br />/path/to/dbmaintain/bin/ update path/to/scriptFolderOrArchive<br />Java code<br />URL configurationUrl = new File("").toURI().toURL();<br />MainFactorymainFactory = new MainFactory(configurationUrl);<br />DbMaintainerdbMaintainer = createDbMaintainer();<br />dbMaintainer.updateDatabase(false);<br />
  16. 16. DbMaintain Commands<br />createScriptArchive<br />updateDatabase<br />markErrorScriptPerformed<br />markErrorScriptReverted<br />markDatabaseAsUpToDate<br />checkScriptUpdates<br />clearDatabase<br />cleanDatabase<br />disableConstraints<br />updateSequences<br />
  17. 17. DbUnit Integration<br />Small test datasets<br />Only needed fields present<br />XSD or DTD for XML datasets validation and autocompletion<br />
  18. 18. DbUnit Integration<br />Granular datasets on class or test level<br />Flexible datasets naming policy<br />Test and expected datasets<br />Multiple datasets support<br />
  19. 19. EasyMock Integration<br />TestedObject concept<br />Easy mocks creation with annotations<br />Completely configurable mocks<br />Extended set of parameter conditions<br />Injection of mocks<br />
  20. 20. Own Mocking Library<br />
  21. 21. Own Mocking Library<br />Can be used in parallel with EasyMock<br />Mocking style like in Mockito<br />Dummy objects<br />Partial mocks<br />
  22. 22. Spring Integration<br />Context loading with caching<br />Different types of Spring bean injection<br />Ability to override beans by loading multiple contexts<br />
  23. 23. Hibernate Integration<br />Special test for mapping consistency<br />Easy to create and inject SessionFactory<br />
  24. 24. JPA Integration<br />Supports Hibernate, Toplink, OpenJPA<br />JpaEntityManagerFactory creates EntityManager for each test<br />JPA utilities for injection and mapping validation<br />
  25. 25. Assertion Utilities<br />Reflection based<br />Flexible comparison<br />Ignore types of numbers and collections<br />Ignore order in collections<br />Ignore properties<br />Able to compare only some properties<br />
  26. 26. Assertion Utilities<br />Supports object hierarchy<br />Great differences reporting<br />Lenient by default<br />
  27. 27. Unitils Failures<br /><ul><li>First record in DbUnit dataset should have all columns used further
  28. 28. Conditions must be set for all parameters in method when using EasyMock
  29. 29. EasyMock error reports are hard to understand</li></ul>Many bugs in bug tracker<br />
  30. 30. Any questions?<br />