Generic DAO Testing via TestGenericDAO

November 5, 2006

The toybank project has many DAO classes. All these DAO classes extend GenericDAO and hence inherit the methods save, delete, findAll, findById, findByExample, findByCriteria. Therefore, we have implemented the TestGenericDAO test class which tests all those generic methods. TestGenericDAO is an abstract class which extends TestCase and tests all the methods provided by GenericDAO. The test classes for the DAO classes that extend GenericDAO should extend TestGenericDAO.

In order to extend TestGenericDAO, you need to implement the following five abstract methods:

protected abstract GenericDAO<PersistentType, IDType> getDAO();

protected abstract IDType getId(PersistentType persistentObject);

protected abstract boolean areIdentical(PersistentType persistentObject, PersistentType anotherPersistentObject);

protected abstract List<PersistentType> getTestingSamples();

protected abstract Map<Example, List<Integer>> getExamplesWithTestResults();

protected abstract Map<List<Criterion>, List<Integer>> getCriterionWithTestResults();

All these methods are well commented. The comments clearly explain the purpose/requirements for the implementations. You can look at TestPersonDAO for an example of how to implement and use the abstract methods.

The setUp method in TestGenericDAO initializes a protected instance of the appropriate DAO class. This is the instance that should be used in all the test methods. The tearDown method closes the session used by the protected DAO instance. This ensures that no two tests affect each other by using the same session.

An important requirement for all the test methods in any test class that extends TestGenericDAO is that they should leave the database at the same state that it was in at the start of the method. i.e. All the test methods should clean up all the changes that they make to the database. The generic test methods that are provided by TestGenericDAO follow this rule. You can look at them to see how it is done.

Another requirement for using TestGenericDAO is the existence of a database with the name testtoybank. This is the database on which all the tests are run. This database can be created by running the test-init-db ant task in build.xml. The test-init-db task checks to see if there is a database by the name testtoybank, if there isnt one, it creates one. Then it creates all the tables required for toybank in testtoybank. The sql script for creating the testdatabse is in test-db-schema.ddl and the sql script for creating the tables is in tables-schema.ddl. We have also changed the init-db task to use a similar process. So init-db now runs db-schema.dll, tables-schema.dll (common to both the tasks), sample.sql and gl_insert_accounts.sql.

Advertisement

2 Responses to “Generic DAO Testing via TestGenericDAO”

  1. Andrew said

    Could you tell me where I could obtain the source for this TestGenericDAO?

    Thanks

  2. As you know, the source is part of a project for a university course. I am not sure whether I can give the source to you. I will have to ask the course organizer about it. Are you serious about wanting the source? If so, I will find out if I can give it to you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.