<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    風人園

    弱水三千,只取一瓢,便能解渴;佛法無邊,奉行一法,便能得益。
    隨筆 - 99, 文章 - 181, 評論 - 56, 引用 - 0
    數據加載中……

    使用spring-mock進行dao集成測試

    在進行dao的集成測試時候,數據清理,察看數據都是比較麻煩的事情,使用Spring-mock.jar可以幫助我們簡化著一個過程。我舉一個簡單的例子,說明一下如何使用spring-mock。

    首先是po, hbm.xml, dao, daoimpl沒什么好說的:
    java代碼:?


    Customer.java :

    package rst.spring.mock;

    import java.io.Serializable;

    /** @author Hibernate CodeGenerator */
    publicclass Customer implementsSerializable{

    ? ? /** identifier field */
    ? ? privateLong id;

    ? ? /** nullable persistent field */
    ? ? privateString name;

    ? ? /** full constructor */
    ? ? public Customer(String name){
    ? ? ? ? this.name = name;
    ? ? }

    ? ? /** default constructor */
    ? ? public Customer(){
    ? ? }

    ? ? publicLong getId(){
    ? ? ? ? return this.id;
    ? ? }

    ? ? publicvoid setId(Long id){
    ? ? ? ? this.id = id;
    ? ? }

    ? ? publicString getName(){
    ? ? ? ? return this.name;
    ? ? }

    ? ? publicvoid setName(String name){
    ? ? ? ? this.name = name;
    ? ? }

    }

    Customer.hbm.xml :

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC
    ? ? ? ? "-//Hibernate/Hibernate Mapping DTD//EN"
    ? ? ? ? "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
    <hibernate-mapping package="rst.spring.mock">
    ? ? ? ? <class name="Customer" table="customer">
    ? ? ? ? ? ? ? ? <id name="id" column="id" type="long" unsaved-value="null">
    ? ? ? ? ? ? ? ? ? ? ? ? <generator class="identity"/>
    ? ? ? ? ? ? ? ? </id>
    ? ? ? ? ? ? ? ? <property name="name" column="name" type="string"/>
    ? ? ? ? </class>

    </hibernate-mapping>

    CustomerDAO :
    /*
    * Created on 2005-3-25
    */

    package rst.spring.mock;

    import org.springframework.dao.DataAccessException;

    /**
    * @author rst
    *
    */

    publicinterface CustomerDAO {
    ? ? publicvoid add(Customer customer)throws DataAccessException;
    }

    CustomerDAOImpl :

    package rst.spring.mock;

    import org.springframework.dao.DataAccessException;
    import org.springframework.orm.hibernate.support.HibernateDaoSupport;

    /**
    * Class description.
    *
    * @author rst
    */

    publicclass CustomerDAOHibernateImpl extends HibernateDaoSupport implements CustomerDAO{
    ? ?
    ? ? publicvoid add(Customer customer)throws DataAccessException{
    ? ? ? ? this.getHibernateTemplate().save(customer);
    ? ? }
    }




    然后測試的基類SpringDAOTestCase繼承自AbstractTransactionalDataSourceSpringContextTests,目前只有一個指定測試用xml文件位置的邏輯。
    java代碼:?


    package rst.spring.mock;

    import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;

    /**
    * Class description.
    *
    * @author rst
    */

    publicabstractclass SpringDAOTestCase extends AbstractTransactionalDataSourceSpringContextTests {

    ? protectedString[] getConfigLocations(){
    ? ? returnnewString[]{ "test.xml" };
    ? }

    }



    接著是我們真正測試的類CustomerDAOTest.java:
    java代碼:?


    package rst.spring.mock;

    /**
    * Class description.
    *
    * @author rst
    */

    publicclass CustomerDaoTest extends SpringDAOTestCase {

    ? ? private CustomerDAOHibernateImpl customerDAO;

    ? ? protectedvoid onSetUpInTransaction()throwsException{
    ? ? ? ? super.onSetUpInTransaction();
    ? ? ? ? //this.setPopulateProtectedVariables(true);
    ? ? ? ? customerDAO = (CustomerDAOHibernateImpl) this.applicationContext.getBean("customerDAO");
    ? ? }

    ? ? protectedvoid onTearDownInTransaction(){
    ? ? ? ? customerDAO = null;
    ? ? }

    ? ? publicvoid testInsert(){
    ? ? ? ? Customer customer = new Customer();
    ? ? ? ? customer.setName("javaeye");
    ? ? ? ? customerDAO.add(customer);
    ? ? ? ? String name = (String) jdbcTemplate.queryForObject("select name from customer where id=?", newObject[]{customer.getId()}, String.class);
    ? ? ? ?
    ? ? ? ? assertEquals(customer.getName(), name);
    ? ? }

    }



    最后看看配置文件test.xml:
    java代碼:?


    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

    <!--
    ? - Application context definition for Petclinic on Hibernate.
    ? ? ? ? -->
    <beans>

    ? ? ? ? <!-- ========================= RESOURCE DEFINITIONS ========================= -->
    ?
    ? ? ? ? <!-- Configurer that replaces ${...} placeholders with values from a properties file -->
    ? ? ? ? <!-- (in this case, JDBC-related settings for the dataSource definition below) -->
    ? ? ? ? <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    ? ? ? ? ? ? ? ? <property name="location"><value>classpath:jdbc.properties</value></property>
    ? ? ? ? </bean>

    ? ? ? ? <!-- Local DataSource that works in any environment -->
    ? ? ? ? <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    ? ? ? ? ? ? ? ? <property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
    ? ? ? ? ? ? ? ? <property name="url"><value>${jdbc.url}</value></property>
    ? ? ? ? ? ? ? ? <property name="username"><value>${jdbc.username}</value></property>
    ? ? ? ? ? ? ? ? <property name="password"><value>${jdbc.password}</value></property>
    ? ? ? ? </bean>

    ? ? ? ? <!-- Hibernate SessionFactory -->
    ? ? ? ? <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
    ? ? ? ? ? ? ? ? <property name="dataSource"><ref local="dataSource"/></property>
    ? ? ? ? ? ? ? ? <property name="mappingResources">
    ? ? ? ? ? ? ? ? ? ? ? ? <value>rst/spring/mock/Customer.hbm.xml</value>
    ? ? ? ? ? ? ? ? </property>
    ? ? ? ? ? ? ? ? <property name="hibernateProperties">
    ? ? ? ? ? ? ? ? ? ? ? ? <props>
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <prop key="hibernate.dialect">${hibernate.dialect}</prop>
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <prop key="hibernate.show_sql">true</prop>
    ? ? ? ? ? ? ? ? ? ? ? ? </props>
    ? ? ? ? ? ? ? ? </property>
    ? ? ? ? </bean>

    ? ? ? ? <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
    ? ? ? ? <bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
    ? ? ? ? ? ? ? ? <property name="sessionFactory"><ref local="sessionFactory"/></property>
    ? ? ? ? </bean>
    ? ? ? ?
    ? ? ? ? <bean id="hibernateTemplate" class="org.springframework.orm.hibernate.HibernateTemplate">
    ? ? ? ? ? ? ? ? <property name="sessionFactory"><ref local="sessionFactory"/></property>
    ? ? ? ? </bean>

    ? ? ? ? <bean id="customerDAO" class="rst.spring.mock.CustomerDAOHibernateImpl">
    ? ? ? ? ? ? ? ? <property name="hibernateTemplate"><ref local="hibernateTemplate"/></property>
    ? ? ? ? </bean>
    </beans>


    這個文件很簡單,不要忘記transactionManager的配置,Test類會自動裝配的。

    運行之后,就可以看到應有的結果,并且數據庫中不會有數據污染。這個過程主要是開始一個transaction,然后開始你的test方法,執行dao操作,執行sql查詢驗證結果,最后無論成功失敗rollback transaction。

    posted on 2006-06-21 12:41 風人園 閱讀(659) 評論(0)  編輯  收藏 所屬分類: Spring

    主站蜘蛛池模板: 一级黄色免费毛片| 亚洲国产成人无码AV在线影院| 亚洲色偷偷狠狠综合网| 亚洲国产精品一区二区成人片国内| 国产AV无码专区亚洲精品| 久久国产一片免费观看| 中文字幕天天躁日日躁狠狠躁免费| 在线视频精品免费| 亚洲国产精品成人一区| 亚洲嫩模在线观看| 色窝窝亚洲av网| 成人无码a级毛片免费| 成人au免费视频影院| 久久夜色精品国产亚洲av| ass亚洲**毛茸茸pics| 一个人晚上在线观看的免费视频| 亚洲AV无码一区二区三区在线观看| 日韩在线观看免费完整版视频| 91视频免费网址| 波多野结衣亚洲一级| 欧洲精品99毛片免费高清观看| 深夜国产福利99亚洲视频| 亚洲国产成AV人天堂无码| 国产日韩一区二区三免费高清| 亚洲AV成人精品网站在线播放| 四虎影视久久久免费| 亚洲国产成人久久精品动漫| 国产黄色免费网站| 美女裸免费观看网站| 成人超污免费网站在线看| 亚洲欧洲日产国码久在线观看| 在线看片v免费观看视频777| 国产精品亚洲一区二区三区在线观看 | 国产真实伦在线视频免费观看| 亚洲人成在线影院| 四虎影视大全免费入口| 国产精品无码永久免费888| 国产又大又黑又粗免费视频| 中文字幕版免费电影网站| 亚洲欧洲一区二区三区| 国产亚洲精品仙踪林在线播放|