I summarized it from an appfuse project.

Section 1.

Any DAO?test suit should inherit the object of AbstractTransactionalDataSourceSpringContextTests, which is used to test a data source.

?There might be many DAO objects to be tested in a project. Those DAO objects share the same configuration. Therefore, the best way is to create a new an abstract class BaseDaoTestCase extends AbstractTransactionalDataSourceSpringContextTests. The method ?of getConfigLocations() must be implemented, which is to return a string array including the configuration files such as “applicationContext-hibernate.xml”, “applicationContext-resources.xml” and “applicationContext-service.xml”, etc. Those files can be used for many sub DAO test suits. Here is an example of BaseDaoTestCase:


package
com.pplus.test.dao;

?

import java.util.Enumeration;

import java.util.HashMap;

import java.util.Map;

import java.util.MissingResourceException;

import java.util.ResourceBundle;

?

import org.apache.commons.beanutils.BeanUtils;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;

?

/**

? * Base class for running Dao tests.

? * @author mraible

? */

?

public abstract class BaseDaoTestCase extends AbstractTransactionalDataSourceSpringContextTests {

??? protected final Log log = LogFactory.getLog(getClass());

??? protected ResourceBundle rb ;

?

??? protected String[] getConfigLocations() {

??????? setAutowireMode( AUTOWIRE_BY_NAME );

??????? String dir= "file:D:/yin/CN2.0/CN21/WEB-INF/" ;

??????? //*

??????? return new String [] {dir+ "applicationContext-hibernate.xml" ,

??????? ????????? ??????????????dir+ "applicationContext-resources.xml" ,

??????? ????????? ??????????????dir+ "applicationContext-service.xml"

????????????????????????????? };

????????????????????????????? //*/

?

??? }

???

??? public BaseDaoTestCase() {

??????? // Since a ResourceBundle is not required for each class, just

??????? // do a simple check to see if one exists

??????? String className = this .getClass().getName();

?

??????? try {

??????????? rb = ResourceBundle.getBundle(className);

??????? } catch (MissingResourceException mre) {

??????????? //log.warn("No resource bundle found for: " + className);

??????? }

??? }

?

??? /**

???? * Utility method to populate a javabean - style object with values

???? * from a Properties file

???? * @param obj

???? * @return Object populated object

???? * @throws Exception

???? */

??? protected Object populate(Object obj) throws Exception {

??????? // loop through all the beans methods and set its properties from

??????? // its .properties file

??????? Map map = new HashMap();

?

??????? for (Enumeration keys = rb .getKeys(); keys.hasMoreElements();) {

??????????? String key = (String) keys.nextElement();

??????????? map.put(key, rb .getString(key));

??????? }

?

??????? BeanUtils.copyProperties(obj, map);

?

??????? return obj;

??? }

}

?

Section 2.

Creates a TestCase inherits BaseDaoTestCase. Generally, it includes two attributes: an object dao to be tested and an value object which reflects the key fields of the module object.? We must innovate the setter of dao. Here is an example:

package com.pplus.test.dao;

?

?

import java.util.List;

?

import javax.sql.DataSource;

?

import com.pplus.dao.TcnrDriverDao;

import com.pplus.model.TcnrDriver;

?

import org.springframework.orm.ObjectRetrievalFailureException;

?

public class TcnrDriverDaoTest extends BaseDaoTestCase {

??? private Integer tcnrDriverId = new Integer("1");

??? private TcnrDriverDao dao = null;

?

??? public void setTcnrDriverDao(TcnrDriverDao dao) {

??????? this.dao = dao;

??? }

???

??

?

??????????? public void testAddTcnrDriver() throws Exception {

??????? TcnrDriver tcnrDriver = new TcnrDriver();

?

??????? // set required fields

?

??????? dao.saveTcnrDriver(tcnrDriver);

?

??????? // verify a primary key was assigned

??????? assertNotNull(tcnrDriver.getDriverid());

?

??????? // verify set fields are same after save

??? }

?

??? public void testGetTcnrDriver() throws Exception {

??????? TcnrDriver tcnrDriver = dao.getTcnrDriver(tcnrDriverId);

??????? assertNotNull(tcnrDriver);

??? }

?

??? public void testGetTcnrDrivers() throws Exception {

??????? TcnrDriver tcnrDriver = new TcnrDriver();

?

??????? List results = dao.getTcnrDrivers(tcnrDriver);

??????? assertTrue(results.size() > 0);

??? }

?

??? public void testSaveTcnrDriver() throws Exception {

??????? TcnrDriver tcnrDriver = dao.getTcnrDriver(tcnrDriverId);

?

??????? // update required fields

?

??????? dao.saveTcnrDriver(tcnrDriver);

?

??? }

?

??? public void testRemoveTcnrDriver() throws Exception {

??????? Integer removeId = new Integer("3");

??????? dao.removeTcnrDriver(removeId);

??????? try {

??????????? dao.getTcnrDriver(removeId);

??????????? fail("tcnrDriver found in database");

??????? } catch (ObjectRetrievalFailureException e) {

????????? ??assertNotNull(e.getMessage());

??????? }

??? }

}

?

?