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

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

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

    風(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讀取)
    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ù)庫(kù)或者從數(shù)據(jù)庫(kù)刪除
    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)人園 閱讀(1544) 評(píng)論(0)  編輯  收藏 所屬分類: DAO

    主站蜘蛛池模板: 亚洲激情在线观看| 玖玖在线免费视频| 色播亚洲视频在线观看| 免费一本色道久久一区| 亚洲国产一成人久久精品| 最近国语视频在线观看免费播放 | 免费国产不卡午夜福在线| 在线成人爽a毛片免费软件| 日韩一级片免费观看| 国产亚洲色视频在线| 国产性生交xxxxx免费| 国产精品无码永久免费888| 亚洲中文字幕久久精品无码A| 亚洲av无码一区二区三区乱子伦| 精品国产无限资源免费观看| 亚洲国产精品日韩av不卡在线| 中文字幕亚洲色图| 日本成人在线免费观看| 99爱免费观看视频在线| 亚洲av无码片vr一区二区三区| 亚洲精品无码久久千人斩| 五月天婷亚洲天综合网精品偷| 最近2019中文字幕mv免费看| 一级特黄特色的免费大片视频| 亚洲AV无码不卡在线播放| 亚洲日韩在线观看| 免费久久精品国产片香蕉| 女性无套免费网站在线看| 九九九精品成人免费视频| 亚洲一区免费在线观看| 蜜桃视频在线观看免费视频网站WWW| 9i9精品国产免费久久| 野花视频在线官网免费1| 久久亚洲AV无码精品色午夜麻豆| 精品亚洲永久免费精品| 亚洲午夜激情视频| 亚洲人成在线播放网站| 国产亚洲美女精品久久久| 久久亚洲色一区二区三区| a级亚洲片精品久久久久久久| 1000部拍拍拍18勿入免费视频软件 |