Spring inherits from JUnit

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.