風(fēng)人園

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

          Spring DAO 測(cè)試

          今天我將展示一下我是如何在實(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使用比較簡(jiǎn)單的,只需要設(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的簡(jiǎn)單查詢方法
          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讀?。?br />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-18 11:37 風(fēng)人園 閱讀(1548) 評(píng)論(0)  編輯  收藏 所屬分類: DAO

          主站蜘蛛池模板: 益阳市| 阿瓦提县| 西昌市| 太和县| 颍上县| 吉木萨尔县| 台安县| 长宁县| 开远市| 稷山县| 逊克县| 临朐县| 新化县| 广元市| 康马县| 武鸣县| 怀集县| 郧西县| 章丘市| 巴林左旗| 青浦区| 敦煌市| 崇阳县| 扶余县| 镇宁| 汝南县| 青浦区| 阿坝县| 鄢陵县| 石河子市| 乐业县| 云林县| 云南省| 三都| 邹城市| 达孜县| 肥乡县| 营口市| 淄博市| 那曲县| 张北县|