1. Create a base class
Public abstract class basedaotestcase extends acttransactionalspringcontexttests {
Protected final log = logfactory. getlog (getclass ());
Protected resourcebundle RB;
Protected string [] getconfiglocations (){
Setautowiremode (autowire_by_name );
Return New String [] {
"Classpath *:/applicationContext-resources.xml ",
"Classpath *:/META-INF/AOP/**/applicationcontext-AOP *. xml ",
"Classpath *:/META-INF/Dao/**/applicationcontext-Dao *. xml "};
}
Public basedaotestcase (){
// Since a resourcebundle is not required for each class, just
// Do a simple check to see if one existsku
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
* Properties File
*
* @ Param OBJ
* The model object to populate
* @ Return object populated object
* @ Throws exception
* If beanutils fails to copy properly
*/
Protected object populate (Object OBJ) throws exception {
// Loop through all the beans methods and set its properties from
// Its. properties File
Map <string, string> map = new hashmap <string, string> ();
For (enumeration <string> keys = RB. getkeys (); keys. hasmoreelements ();){
String key = keys. nextelement ();
Map. Put (Key, RB. getstring (key ));
}
Beanutils. copyproperties (MAP, OBJ );
Return OBJ;
}
/**
* Create a hibernatetemplate from the sessionfactory and call flush () and
* Clear () on it. Designed to be used after "savesku" methods in tests:
* Http://issues.appfuse.org/browse/APF-178.
*/
Protected void flush (){
Hibernatetemplate = new hibernatetemplate (
(Sessionfactory) applicationcontext. getbean ("sessionfactory "));
Hibernatetemplate. Flush ();
Hibernatetemplate. Clear ();
}
}
2. Create a class to be tested and inherit from 1 (basedaotestcase. Java)
Public class decenterprisedaohibernatetest extends basedaotestcase {
Private decenterprisedaohibernate Dao;
Public void setdecenterprisedao (decenterprisedaohibernate Dao ){
This. Dao = Dao;
}
@ Test
Public void testfind () throws daoexception {
Decenterprisedto DTO = new decenterprisedto ();
List list = NULL;
List = Dao. finddecenterpriselistbydto (DTO );
System. Out. println ("sssss:" + list. Size ());
}
}
Classpath *: A special prefix. All class path resources that match the name after the prefix should be obtained.