随笔-55  评论-208  文章-0  trackbacks-0
今天我将展示一下我是如何在实际中对dao进行单元测试的
首先我们来确认一下dao需要什么样的环境,我的dao是用Spring+hibernate来构建的,而对应的数据源是oracle9。所以要进行dao的测试我需要从Spring的连接oracle的context中获取dao的实例出来,这里我使用的是spring-mock
spring-mock使用比较简单的,只需要设置spring的配置文件路径就可以获得上下文了
这里需要注意的是这个spring上下文是ClassPathApplicationContext,而我们在web环境中经常遇到的是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配置的加载是否正确的

下面给出一个DAO的简单查询方法
public List getHomepageAreasByChannelId(long channelId) {

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


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

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());

    }

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

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

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

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

 

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

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);
    }

其实这种save或者delete的方法由于使用时都是基本调用hibernate的方法,所以在我看来测试的意义并不是很大
posted on 2007-01-17 00:59 rocket 阅读(4092) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: