隨筆-55  評(píng)論-208  文章-0  trackbacks-0
          今天我將展示一下我是如何在實(shí)際中對(duì)dao進(jìn)行單元測(cè)試的
          首先我們來確認(rèn)一下dao需要什么樣的環(huán)境,我的dao是用Spring+hibernate來構(gòu)建的,而對(duì)應(yīng)的數(shù)據(jù)源是oracle9。所以要進(jìn)行dao的測(cè)試我需要從Spring的連接oracle的context中獲取dao的實(shí)例出來,這里我使用的是spring-mock
          spring-mock使用比較簡單的,只需要設(shè)置spring的配置文件路徑就可以獲得上下文了
          這里需要注意的是這個(gè)spring上下文是ClassPathApplicationContext,而我們?cè)趙eb環(huán)境中經(jīng)常遇到的是WebApplicationContext
          /**
           * $Id:$
           *
           * Copyright 2005 easou, Inc. All Rights Reserved.
           
          */

          package test.spring.common;

          import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;

          import test.PathConfig;

          public class BaseSpringTestCase extends
                  AbstractTransactionalDataSourceSpringContextTests 
          {    

              @Override
              
          protected String[] getConfigLocations() {

                  String[] config 
          = PathConfig.springxml;

                  
          return config;

              }
                  
              
          public void testConfig() {        
                  assertNotNull(
          "spring-mock context has bean init()",this.applicationContext);
              }


          }
          這里testConfig是用來檢查你spring配置的加載是否正確的

          下面給出一個(gè)DAO的簡單查詢方法
          public List getHomepageAreasByChannelId(long channelId) {

                  
          return this.executeHQL(" from CsHomepageArea  h where h.csChannel.id='"
                          
          + channelId + "' order by h.theOrder");
              }


          上面的方法指示根據(jù)一個(gè)id取列表出來,而我們要測(cè)試的目標(biāo)有(其實(shí)也就是我們這個(gè)方法要實(shí)現(xiàn)的目標(biāo)):
          1、給出正確的id是否能否返回正確的結(jié)果
          2、返回的結(jié)果集能夠根據(jù)hibernate配置文件而得到我們期望的結(jié)果集(比如說對(duì)子集的lazy讀取)
          3、返回的結(jié)果集是否按照你所期望的排序
          4、給出錯(cuò)誤的id是否在獲取數(shù)據(jù)時(shí)會(huì)出錯(cuò)
          根據(jù)上面的測(cè)試目標(biāo)我們就很容易的得到下面的測(cè)試方法了

          public void testGetHomepageAreasByChannelId() {
                  List list 
          = channelDAO.getHomepageAreasByChannelId(1);
                  assertNotNull(
          "homepage list is not null", list);
                  CsHomepageArea homepage 
          = (CsHomepageArea) list.get(0);
                  assertNotNull(
          "homepage'name is not null", homepage.getName());
                  assertNotNull(
          "homepage'channel has been lazy", homepage.getCsChannel()
                          .getName());
                  assertNotNull(
          "homepage'column has been lazy", homepage.getCsColumn()
                          .getName());
                  assertNotNull(
          "homepage'subject has been lazy", homepage
                          .getCsSubjects().iterator().next().getName());
                  CsSubject subject 
          = (CsSubject) homepage.getCsSubjects().iterator()
                          .next();
                  assertNotNull(
          "homepage'subject'keyword has been lazy", subject
                          .getCsSubjectKeywords().iterator().next().getName());

              }

          對(duì)于DAO層的查詢方法,我們測(cè)試的就是判斷返回的數(shù)據(jù)是否是我們需要的

          下面這個(gè)方法是DAO的增改方法,和刪除方法

          public void saveComment(CsComment comment) {
                  getHibernateTemplate().saveOrUpdate(comment);        
              }

              
          public void deleteComment(CsComment comment) {        
                  getHibernateTemplate().delete(comment);        
              }

           

          對(duì)于這種無返回值得方法我們主要測(cè)試的是:
          1、對(duì)于正確的數(shù)據(jù)是否能夠正確的存入數(shù)據(jù)庫或者從數(shù)據(jù)庫刪除
          2、對(duì)于錯(cuò)誤的數(shù)據(jù)操作能夠有錯(cuò)誤信息(如主鍵重復(fù))

          public void testSaveComment(){
                  CsComment comment 
          = new CsComment();
                  comment.setCommentDate(
          new Date());
                  comment.setContent(
          "comment test");
                  channelDAO.saveComment(comment);
                  CsComment dbComment 
          =(CsComment)channelDAO.getEntity(comment.getId());
                  assertNotNull(
          "comment has bean saved", dbComment);
              }

              
          public void testDeleteComment(){
                  CsComment comment 
          = new CsComment();
                  comment.setId(
          new Long(13));
                  channelDAO.delete(comment);
                  CsComment dbComment 
          =(CsComment)channelDAO.getEntity(comment.getId());
                  assertNull(
          "comment has bean delete", dbComment);
              }

          其實(shí)這種save或者delete的方法由于使用時(shí)都是基本調(diào)用hibernate的方法,所以在我看來測(cè)試的意義并不是很大
          posted on 2007-01-17 00:59 rocket 閱讀(4127) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 新绛县| 鲁山县| 大丰市| 洞口县| 平塘县| 清原| 吉安县| 高台县| 山丹县| 偏关县| 正蓝旗| 女性| 庆阳市| 弥渡县| 宁陵县| 汾阳市| 错那县| 施甸县| 榆树市| 江口县| 鹤峰县| 偏关县| 剑阁县| 双城市| 彭阳县| 绥化市| 沙坪坝区| 扶余县| 桂平市| 墨江| 军事| 开平市| 延安市| 米林县| 紫云| 克拉玛依市| 泌阳县| 诸城市| 汕尾市| 宝山区| 科技|