DAO层单元测试

今天我将展示一下我是如何在实际中对dao进行单元测试的
首先我们来确认一下dao需要什么样的环境,我的dao是用Spring+hibernate来构建的,而对应的数据源是oracle9。所以要进行dao的测试我需要从Spring的连接oracle的context中获取dao的实例出来,这里我使用的是spring-mock
spring-mock使用比较简单的,只需要设置spring的配置文件路径就可以获得上下文了
这里需要注意的是这个spring上下文是ClassPathApplicationContext,而我们在web环境中经常遇到的是WebApplicationContext
ExpandedBlockStart.gif ContractedBlock.gif /** */ /**
InBlock.gif * $Id:$
InBlock.gif *
InBlock.gif * Copyright 2005 easou, Inc. All Rights Reserved.
ExpandedBlockEnd.gif 
*/

None.gif
package  test.spring.common;
None.gif
None.gif
import  org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
None.gif
None.gif
import  test.PathConfig;
None.gif
None.gif
public   class  BaseSpringTestCase  extends
ExpandedBlockStart.gifContractedBlock.gif        AbstractTransactionalDataSourceSpringContextTests 
dot.gif {InBlock.gif    
InBlock.gif
InBlock.gif    @Override
ExpandedSubBlockStart.gifContractedSubBlock.gif    
protected String[] getConfigLocations() dot.gif{
InBlock.gif
InBlock.gif        String[] config 
= PathConfig.springxml;
InBlock.gif
InBlock.gif        
return config;
InBlock.gif
ExpandedSubBlockEnd.gif    }
InBlock.gif    InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void testConfig() dot.gif{        
InBlock.gif        assertNotNull(
"spring-mock context has bean init()",this.applicationContext);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}
这里testConfig是用来检查你spring配置的加载是否正确的

下面给出一个DAO的简单查询方法
ExpandedBlockStart.gif ContractedBlock.gif public  List getHomepageAreasByChannelId( long  channelId)  dot.gif {
InBlock.gif
InBlock.gif        
return this.executeHQL(" from CsHomepageArea  h where h.csChannel.id='"
InBlock.gif                
+ channelId + "' order by h.theOrder");
ExpandedBlockEnd.gif    }


上面的方法指示根据一个id取列表出来,而我们要测试的目标有(其实也就是我们这个方法要实现的目标):
1、给出正确的id是否能否返回正确的结果
2、返回的结果集能够根据hibernate配置文件而得到我们期望的结果集(比如说对子集的lazy读取)
3、返回的结果集是否按照你所期望的排序
4、给出错误的id是否在获取数据时会出错
根据上面的测试目标我们就很容易的得到下面的测试方法了

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

对于DAO层的查询方法,我们测试的就是判断返回的数据是否是我们需要的

下面这个方法是DAO的增改方法,和删除方法

ExpandedBlockStart.gif ContractedBlock.gif public   void  saveComment(CsComment comment)  dot.gif {
InBlock.gif        getHibernateTemplate().saveOrUpdate(comment);        
ExpandedBlockEnd.gif    }

ExpandedBlockStart.gifContractedBlock.gif    
public   void  deleteComment(CsComment comment)  dot.gif {        
InBlock.gif        getHibernateTemplate().delete(comment);        
ExpandedBlockEnd.gif    }

 

对于这种无返回值得方法我们主要测试的是:
1、对于正确的数据是否能够正确的存入数据库或者从数据库删除
2、对于错误的数据操作能够有错误信息(如主键重复)

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

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

其实这种save或者delete的方法由于使用时都是基本调用hibernate的方法,所以在我看来测试的意义并不是很大
94335.html

rocket 2007-01-17 00:59 发表评论
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值