Unitils

27

Transcript of Unitils

Page 2: Unitils

23/12/2007 2

Agenda

Api DescriptionFeaturesUnitils DesignSupported Module SystemUnitils by Example

Page 3: Unitils

23/12/2007 3

Page 4: Unitils

23/12/2007 4

Unitils test Tool

Unitils is an open source library aimed at making unit testing easy and maintainable. Unitils builds further on existing libraries like DBUnit and EasyMock and integrates with JUnit and TestNG.

Unitils provides general asserion utilities, support for database testing, support for testing with mock objects and offers integration with Spring and Hibernate. It has been designed to offer these services to unit tests in a very configurable and loosely coupled way. As a result, services can be added and extended very easily.

Page 5: Unitils

23/12/2007 5

Currently Unitils offers following features

Assertion utils Equality assertion through reflection, with different

options like ignoring Java default/null values and ignoring order of collections

Database testing utilities: Test data management DBUnit Test database maintenance Testing with Hibernate

Page 6: Unitils

23/12/2007 6

Currently Unitils offers followingfeatures

Mock object utilities Simplify EasyMock mock object creation Simplify mock object injection EasyMock argument matching using reflection equality

Spring integration ApplicationContext configuration and easy injection of spring

managed beans into a unit test Support for using a Spring-configured Hibernate SessionFactory

in unit tests.

The project started begin 2006 from an Ordina J-Technologies discussion group on unit testing. The result was a list of guidelines and Unitils emerged in an attempt to write code to support these guidelines.

Page 7: Unitils

23/12/2007 7

Page 8: Unitils

23/12/2007 8

Open & extensible architecture

Module system Listener hooks up modules into tests

Support for JUnit 3 / 4 + TestNG

Page 9: Unitils

23/12/2007 9

Module Systems

Database ModuleDBUnit module Test data management

HibernateModuleHibernate Configuration,Verify database mappingSpringModule Load application Context, retrieve spring beans

EasyMock Module:create mocks,lenient reflections arguments matching.

Page 10: Unitils

23/12/2007 10

Database testing

Control over test data Unit test database without data Small test data sets

Limited sharing of data sets Test class or method level

Page 11: Unitils

23/12/2007 11

Class-level data file

Page 12: Unitils

23/12/2007 12

Method-level data file

Page 13: Unitils

23/12/2007 13

Specify file name explicitly

Page 14: Unitils

23/12/2007 14

Result data file

Page 15: Unitils

23/12/2007 15

Hibernate support

Simplify configurationAutomatically check consistency of mapped classes with database

Page 16: Unitils

23/12/2007 16

Assertion utils

Page 17: Unitils

23/12/2007 17

Maintainability

Only check what’s essential Ignore type of numbers Ignore certain properties Ignore order and type of collections

Page 18: Unitils

23/12/2007 18

Ignore order and type ofCollections

Leniency option: collections

Page 19: Unitils

23/12/2007 19

Unitils Configuration

As with many projects, Unitils needs some configuration of its services. By default, there are 3 levels of configuration, each level overriding the settings of the previous one

unitils-defaults.properties: default configuration that is shipped with the distribution of Unitils unitils.properties: can contain project-wide configuration unitils-local.properties: can contain user-specific configuration

Page 20: Unitils

23/12/2007 20

Page 21: Unitils

23/12/2007 21

Programme a tester

Employees : DTO ObjectIEmployeeDao : interface that declare CRUD opperationsEmployeeDaoImpl :a spring hibernate implementationDataSource.xml :Spring Config DataSourceHibernateSessionFactory.xml: Spring Config of the hibernate SessionFactory Dao.xml : DAO Beans

Page 22: Unitils

23/12/2007 22

Programme de test

HibernateMappingTestEmployeeDaoTest :EmployeeDao Testing ClassUnitils.properties : unitils config file

Page 23: Unitils

23/12/2007 23

Configuration Unitils« unitils.properties »

Page 24: Unitils

23/12/2007 24

HibernateMappingTest

@SpringApplicationContext annotation instructs Unitils to create a Spring ApplicationContext, in which the DataSource.xml and HibernateSessionFactory.xml files are loaded

HibernateUnitils Unitils contains a very simple but powerful mapping test. This test checks whether the mapping of all Hibernate mapped objects is

consistent with the database

Page 25: Unitils

23/12/2007 25

HibernateMappingTest « propety title of class Employees result goes titleD »

junit.framework.AssertionFailedError: Found mismatches between Java objects and database tables. Applying following DDL statements to the database should resolve the problem:

alter table public.employees add column titleD varchar(0);

Page 26: Unitils

23/12/2007 26

EmployeeDaoTest

1. Creating an application context and retrieving a Spring bean

3. we tell the system to find a bean of type employeeDao in the application context

5. Loading a test data set

10. Checking the result

Page 27: Unitils

23/12/2007 27

Links

Unitils website:http://unitils.sourceforge.net

Unit testing guidelines:http://unitils.sourceforge.net/guidelines