Spring中使用HSQLDB测试ibatis的dao 博客分类: Spring DAOiBATISHSQLDBSpringCMS
项目是用Spring+ibatis+jsp开发,一直再看敏捷开发方面的文章,虽然没有做到TDD,也没有做到事后测试,和每个方法都测试,但是也是在一直向这个方向努力,也在努力构建可测试的系统而努力。以前都是在测试一些工具类,现在努力在各个方面构建测试,努力做到自己的成果都是测试过的。 一直以来,数据库方面的测试都是比较困难的,特别是一些SQL语句是否正确,还有就是数据库中数据的不稳定性导致测试不可预见性。
从网上找了很多文章,都是HSQLDB测试Hibernate的 ,ibatis的找了很多都没有找到,只能自己一点点摸索,现在基本上稳定了,也可以了。写下来,经验共享一下,也让大家指点一下。废话少说,上代码。
结构Controller + logic(业务代码)+DAO(基本上只用一个工具类DAO)
- /**
- *数据访问类,所有对数据库访问的DAO都应继承这个抽象类,
- *可以利用其中一些公用方法
- */
- public class DefaultDAO extends SqlMapClientDaoSupport {
Logic基本上就是调用DAO进行一些操作,和一些业务代码,没有什么可介绍的。
下面介绍测试logic的测试基类
- import java.io.Reader;
- import junit.framework.TestCase;
- import org.springframework.jdbc.datasource.DriverManagerDataSource;
- import org.springframework.jdbc.datasource.SingleConnectionDataSource;
- import org.springframework.orm.ibatis.SqlMapClientFactoryBean;
- import propertity.config.SystemConfig;
- import com.ibatis.common.resources.Resources;
- import com.ibatis.sqlmap.client.SqlMapClient;
- import com.ibatis.sqlmap.client.SqlMapClientBuilder;
- public abstract class DefaultLogicTest extends TestCase {
- private SingleConnectionDataSource m_dataSource;
- private SqlMapClient m_sqlMapClient; // Actually initialized in superclass
- private DefaultDAO m_dao;
- protected void setUp() throws Exception {
- super.setUp();
- // 构造DataSource
- m_dataSource = new SingleConnectionDataSource();
- m_dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
- m_dataSource.setUrl("jdbc:hsqldb:file:"+ SystemConfig.WORKSPACE_PATH + getDBScriptPath());
- m_dataSource.setUsername("sa");
- m_dataSource.setPassword("");
- m_dataSource.getConnection().setAutoCommit(false);
- m_dataSource.setSuppressClose(false);
- // 构在SQLMapClient
- Reader reader = Resources.getResourceAsReader(SystemConfig.SQL_MAP_CONFIG_RESOURCE);
- m_sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
- m_sqlMapClient.setUserConnection(m_dataSource.getConnection());
- // 构造默认的DAO
- m_dao = new DefaultDAO();
- m_dao.setDataSource(m_dataSource);
- m_dao.setSqlMapClient(m_sqlMapClient);
- }
- /**
- * 得到DAO
- * @return
- */
- protected DefaultDAO getDao() {
- return m_dao;
- }
- /**
- * 得到数据库的位置
- * @return
- */
- protected String getDBScriptPath() {
- return "/com/aaaa/bbbb/cccc/dddd/action/map/action";
- }
- /* (non-Javadoc)
- * @see junit.framework.TestCase#tearDown()
- */
- protected void tearDown() throws Exception {
- super.tearDown();
- m_sqlMapClient.setUserConnection(m_dataSource.getConnection());
- m_sqlMapClient.update("HsqlDB.shutdown", null);
- m_dataSource.getConnection().close();
- m_sqlMapClient.setUserConnection(null);
- }
- }
这个里面需要注意的就是那个得到数据库位置的方法,因为我为每个测试类指定不同的数据库内容,如果不写就默认的,需要写数据库文件后面的.script后缀。
还有就是那个tearDown中的【shutdown】sql,因为Hsqldb如果不调用这句话的话,就会改变Script里面的内容,这个让我困惑了一阵子:) ,还有就是Url中的SystemConfig.WORKSPACE_PATH 常量,主要是自己找了半天资料,也找不到相对路径的写法,于是就定义了一个常量
下面就是具体的测试类
- public class UserLogicTest extends DefaultLogicTest {
- private UserLogic logic;
- protected void setUp() throws Exception {
- super.setUp();
- logic = new UserLogic();
- logic.setDefaultDao(super.getDao());
- }
- /**
- * 没有检索用户销售区域
- */
- public void testQueryUserByName() {
- User user = new User();
- user.setCorpID("1006");
- user.setUserID("PK116055807175866885");
- user.setLoginName("kaka");
- user.setPassword("1q");
- User userDB = logic.queryUserByName(user);
- assertNotNull("销售区域", userDB.getSaleAreaCls());
- }
- /**
- * 维护用户没有更新用户销售区域
- */
- public void testMaintenUser() throws Exception{
- User user = new User();
- user.setCorpID("1006");
- user.setUserID("PK116055807175866885");
- user.setPassword("password");
- user.setSaleAreaCls("0");
- logic.maintenUser(user);
- User userDB = logic.queryUserByID(user);
- assertEquals("销售区域", "0", userDB.getSaleAreaCls());
- }
- /**
- * 测试修改密码
- */
- public void testSaveUserPwd() throws Exception {
- User user = new User();
- user.setCorpID("1006");
- user.setUserID("PK116055807175866885");
- user.setPassword("password");
- // 插入前判断一下
- User userDB = logic.queryUserByID(user);
- assertFalse("密码开始", user.getPassword().equals(userDB.getPassword()));
- logic.saveUserPwd(user);
- userDB = logic.queryUserByID(user);
- assertEquals("密码结束", userDB.getPassword(), user.getPassword());
- }
- /**
- * 测试删除用户
- *
- */
- public void testDeleteUserByPK() throws Exception {
- User user = new User();
- user.setUserID("PK116055807175866884");
- logic.deleteUserByPK(user.getUserID());
- User userDB = logic.queryUserByID(user);
- assertNull("成功删除用户", userDB);
- super.getDao().getDataSource().getConnection().rollback();
- }
- public void testQueryUserRelationAndNotGroupListMap() {
- User user = new User();
- user.setUserID("PK116055807175866885");
- Map groupMap = logic.queryUserRelationAndNotGroupListMap(user.getUserID());
- List relationGroupList = (List)groupMap.get("RELATION");
- List notRelationGroupList = (List)groupMap.get("NOT_RELATION");
- // 关联的
- assertEquals("关联的是3个", 3, relationGroupList.size());
- // 顺序 1,2,test
- testSeq(new String[]{"1", "2", "test"}, relationGroupList);
- // 没有关联的
- assertEquals("没有关联的是4个", 4, notRelationGroupList.size());
- testSeq(new String[]{"原燃料供应商组", "综合供应商组", "销售客户组", "系统测试"}, notRelationGroupList);
- }
- private void testSeq(String[] seqNem, List groupList) {
- for (int i = 0; i < seqNem.length; i++) {
- UserGroup vo = (UserGroup)groupList.get(i);
- assertEquals("名称" + i,seqNem[i], vo.getUserGroupName());
- }
- }
- /**
- * 检查排序问题,大写字母的排在前面
- *
- */
- public void testQueryAllUsers() {
- List userList = logic.queryAllUsers("1006");
- User user = (User)userList.get(0);
- assertEquals("第一个不是大写", "kaka", user.getLoginName());
- }
- /**
- * 保存用户和用户组关联
- *
- */
- public void testSaveRelation() throws Exception {
- RelationGroupFormVO vo = new RelationGroupFormVO();
- vo.setUserID("PK116055807175866885");
- vo.setPk_corp("1006");
- vo.setRel_sel(new String[]{"PK116441420549454662", "PK116458716029826288"});
- logic.saveRelation(vo);
- Map groupMap = logic.queryUserRelationAndNotGroupListMap(vo.getUserID());
- List relationList = (List)groupMap.get("RELATION");
- assertEquals("长度", 2, relationList.size());
- if (relationList.size() <= 0) {
- return;
- }
- testSeq(new String[]{"综合供应商组", "销售客户组"}, relationList);
- }
- /* (non-Javadoc)
- * */
- protected String getDBScriptPath() {
- return "/com/ufida/cvms/basemanagement/usermanager/map/user";
- }
- }
还有就是数据库文件了
- CREATE TABLE BD_CUBASDOC(PK_CUBASDOC CHAR(20) NOT NULL,PK_CORP CHAR(4) NOT NULL,CUSTCODE VARCHAR(30) NOT NULL,CUSTNAME VARCHAR(100) NOT NULL,CUSTSHORTNAME VARCHAR(40) NOT NULL)
- CREATE TABLE CMS_DATADICT(PK_DATADICT CHAR(20) NOT NULL,VTYPE VARCHAR(20),IVALUE INTEGER,CLOCALE CHAR(5) DEFAULT 'zh_CN',VVALUEDESC VARCHAR(64),TS CHAR(19),DR INTEGER DEFAULT 0,VTYPENAME VARCHAR(30))
- CREATE TABLE BD_AREACL(PK_AREACL CHAR(20) NOT NULL,PK_CORP CHAR(4) NOT NULL,AREACLCODE VARCHAR(12) NOT NULL,AREACLNAME VARCHAR(20) NOT NULL,PK_FATHERAREA CHAR(20),TS CHAR(19),DR INTEGER,DEF1 VARCHAR(100),DEF2 VARCHAR(100),DEF3 VARCHAR(100),DEF4 VARCHAR(100),DEF5 VARCHAR(100),MNECODE VARCHAR(10))
- CREATE TABLE CMS_USER(PK_USER CHAR(20) NOT NULL,VLOGINNAME VARCHAR(28),VTRUENAME VARCHAR(32),VPASSWORD VARCHAR(30),VCERTCODE VARCHAR(50),VTEL VARCHAR(30),CCUBASID CHAR(20),CCUMANID CHAR(20),ISALEAREACLS INTEGER,VMOBILE VARCHAR(30),VFAX VARCHAR(30),VEMAIL VARCHAR(50),VMEMO VARCHAR(100),VCORPFULLNAME VARCHAR(128),VCORPSHORTNAME VARCHAR(64),VJURIDICAL VARCHAR(50),VCOUNTRY VARCHAR(64),CAREACLSID CHAR(20),VADDRESS VARCHAR(256),VPOSTALCODE CHAR(6),VTAXNO VARCHAR(32),VMASTERBUSS VARCHAR(128),VBANKNAME VARCHAR(256),VBANKACCOUNT VARCHAR(20),PK_CORP CHAR(4),DACTIVEDATE CHAR(10),DINVALIDATE CHAR(10),IUSERSTATUS INTEGER,DLASTLOGINDATE CHAR(19),IUSERTYPE INTEGER,TS CHAR(19),DR INTEGER)
- CREATE TABLE CMS_USERGROUP(PK_USERGROUP CHAR(20) NOT NULL,VGROUPCODE VARCHAR(28),VGROUPNAME VARCHAR(50),VGROUPDES VARCHAR(50),PK_CORP CHAR(4),TS CHAR(19),DR INTEGER DEFAULT 0)
- CREATE TABLE CMS_USER_GROUP_RELATION(PK_USERGROUP_B CHAR(20) NOT NULL,CUSERGROUPID CHAR(20),CUSERID CHAR(20),TS CHAR(19),DR INTEGER DEFAULT 0)
- CREATE USER SA PASSWORD "" ADMIN
- INSERT INTO BD_CUBASDOC VALUES('0001AA1000000003VQGO','0001','1101649','\u4e2d\u56fd\u77ff\u4ea7\u6709\u9650\u8d23\u4efb\u516c\u53f8','\u4e2d\u56fd\u77ff\u4ea7')
- INSERT INTO CMS_DATADICT VALUES('444 ','YHZT',2,'zh_CN','\u6b63\u5f0f\u7528\u6237','2006-10-17 11:22:26',0,'\u7528\u6237\u72b6\u6001')
- INSERT INTO BD_AREACL VALUES('dqda0000000000000002','0001','1101','\u5317\u4eac\u5e02\u533a','dqda0000000000000001','2002-12-18 00:00:00',0,NULL,NULL,NULL,NULL,NULL,NULL)
- INSERT INTO CMS_USER VALUES('PK116055807175866875','KZka','ABC','aaipjgbcboobnhbc','6666666','6666666666',NULL,NULL,0,'66666666666','11111111666','aaa@saf.com',NULL,'\u56db\u5927\u53d1\u9001\u996d','\u963f\u65af\u8482\u82ac','\u5b8c\u5168\u989d','\u5916\u8033\u5b8c\u5168\u989d','dqda0000000000000002','werewewr','666666','6666666666666','6666666666','66666666666','66666','1006',NULL,NULL,2,'2006-11-24 16:30:44',4,'2006-11-24 16:30:44',0)
- INSERT INTO CMS_USER VALUES('PK116055807175866885','kaka','zjy','aaipjgbcboobnhbc','6666666','6666666666',NULL,NULL,0,'66666666666','11111111666','aaa@saf.com',NULL,'\u56db\u5927\u53d1\u9001\u996d','\u963f\u65af\u8482\u82ac','\u5b8c\u5168\u989d','\u5916\u8033\u5b8c\u5168\u989d','dqda0000000000000002','werewewr','666666','6666666666666','6666666666','66666666666','66666','1006',NULL,NULL,2,'2006-11-24 16:30:44',4,'2006-11-24 16:30:44',0)
- INSERT INTO CMS_USERGROUP VALUES('PK116169875984005137','3','2','1','1006','2006-10-24 21:55:12',0)
- INSERT INTO CMS_USERGROUP VALUES('PK116433523110959947','1','1','1','1006','2006-11-24 10:30:07',0)
- INSERT INTO CMS_USERGROUP VALUES('PK116441420549454662','0003','\u7efc\u5408\u4f9b\u5e94\u5546\u7ec4',NULL,'1006','2006-11-25 09:23:02',0)
- INSERT INTO CMS_USERGROUP VALUES('PK116458716029826288','0011','\u9500\u552e\u5ba2\u6237\u7ec4',NULL,'1006','2006-11-27 09:25:45',0)
- INSERT INTO CMS_USERGROUP VALUES('PK116354770995805209','444','\u7cfb\u7edf\u6d4b\u8bd5','\u521d\u671f\u6d4b\u8bd5\u2015\u2015\u4fe1\u606f\u8bc4\u4ef7\u90e8','1006','2006-11-15 08:40:46',0)
- INSERT INTO CMS_USERGROUP VALUES('PK116354840433039098','0001','\u539f\u71c3\u6599\u4f9b\u5e94\u5546\u7ec4','\u539f\u71c3\u6599\u4f9b\u5e94\u5546','1006','2006-11-15 08:52:20',0)
- INSERT INTO CMS_USERGROUP VALUES('PK116354868037463911','test','test',NULL,'1006','2006-11-15 08:56:56',0)
- INSERT INTO CMS_USER_GROUP_RELATION VALUES('PK116461221779950435','PK116433523110959947','PK116349434520962752','2006-11-27 15:25:44',0)
- INSERT INTO CMS_USER_GROUP_RELATION VALUES('PK116461221779925183','PK116433523110959947','PK116349504077321401','2006-11-27 15:25:44',0)
- INSERT INTO CMS_USER_GROUP_RELATION VALUES('PK116461221779908017','PK116433523110959947','PK116063874195957630','2006-11-27 15:25:44',0)
- INSERT INTO CMS_USER_GROUP_RELATION VALUES('PK116461221779949238','PK116433523110959947','PK116229489004712130','2006-11-27 15:25:44',0)
- INSERT INTO CMS_USER_GROUP_RELATION VALUES('PK116461221779916855','PK116433523110959947','PK116363778699299950','2006-11-27 15:25:44',0)
- INSERT INTO CMS_USER_GROUP_RELATION VALUES('PK116441283966919281','PK116354840433039098','PK116424362932510299','2006-11-25 09:00:16',0)
- INSERT INTO CMS_USER_GROUP_RELATION VALUES('PK116458717250926033','PK116458716029826288','PK116458688918253943','2006-11-27 09:25:57',0)
- INSERT INTO CMS_USER_GROUP_RELATION VALUES('PK116354777072528935','PK116354770995805209','PK116349423878288523','2006-11-15 08:41:46',0)
- INSERT INTO CMS_USER_GROUP_RELATION VALUES('PK116354777072528436','PK116354770995805209','PK116349434520962752','2006-11-15 08:41:46',0)
- INSERT INTO CMS_USER_GROUP_RELATION VALUES('PK116354851343863279','PK116169875984005137','PK116349504077321401','2006-11-15 08:54:09',0)
- INSERT INTO CMS_USER_GROUP_RELATION VALUES('PK116424456759089179','PK116354868037463911','PK116349434520962752','2006-11-23 10:15:34',0)
- INSERT INTO CMS_USER_GROUP_RELATION VALUES('PK116424456759059525','PK116354868037463911','PK116424376929524473','2006-11-23 10:15:34',0)
- INSERT INTO CMS_USER_GROUP_RELATION VALUES('PK116424456759049917','PK116354868037463911','PK116219721663024806','2006-11-23 10:15:34',0)
- INSERT INTO CMS_USER_GROUP_RELATION VALUES('PK116424456759090900','PK116354868037463911','PK116349504077321401','2006-11-23 10:15:34',0)
- INSERT INTO CMS_USER_GROUP_RELATION VALUES('PK116424456759076081','PK116354868037463911','PK116063874195957630','2006-11-23 10:15:34',0)
- INSERT INTO CMS_USER_GROUP_RELATION VALUES('PK116424456759075198','PK116354868037463911','PK116229489004712130','2006-11-23 10:15:34',0)
- INSERT INTO CMS_USER_GROUP_RELATION VALUES('PK116424456759033542','PK116354868037463911','PK116349423878288523','2006-11-23 10:15:34',0)
- INSERT INTO CMS_USER_GROUP_RELATION VALUES('PK116424456759085854','PK116354868037463911','PK116055807175866885','2006-11-23 10:15:34',0)
- INSERT INTO CMS_USER_GROUP_RELATION VALUES('PK116354851343860249','PK116169875984005137','PK116055807175866885','2006-11-15 08:54:09',0)
- INSERT INTO CMS_USER_GROUP_RELATION VALUES('PK116461221779999473','PK116433523110959947','PK116055807175866885','2006-11-27 15:25:44',0)
HSQLDB就是从这个*.script文件里面读入表和数据,然后就可以调用各种方法测试里面的数据。需要注意的是,测试类找不到WEB-INF下面的Sql_Map_Config文件,所以拷贝了一份,放到了测试源文件的包根目录下。
大体上就是这些了,写下来,记录一下,也为这段时间的辛勤劳作一个小小的表彰:)