Unitils

Preview:

Citation preview

tarek.akik@oxiasoft.com

23/12/2007 2

Agenda

Api DescriptionFeaturesUnitils DesignSupported Module SystemUnitils by Example

23/12/2007 3

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.

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

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.

23/12/2007 7

23/12/2007 8

Open & extensible architecture

Module system Listener hooks up modules into tests

Support for JUnit 3 / 4 + TestNG

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.

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

23/12/2007 11

Class-level data file

23/12/2007 12

Method-level data file

23/12/2007 13

Specify file name explicitly

23/12/2007 14

Result data file

23/12/2007 15

Hibernate support

Simplify configurationAutomatically check consistency of mapped classes with database

23/12/2007 16

Assertion utils

23/12/2007 17

Maintainability

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

23/12/2007 18

Ignore order and type ofCollections

Leniency option: collections

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

23/12/2007 20

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

23/12/2007 22

Programme de test

HibernateMappingTestEmployeeDaoTest :EmployeeDao Testing ClassUnitils.properties : unitils config file

23/12/2007 23

Configuration Unitils« unitils.properties »

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

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);

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

23/12/2007 27

Links

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

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