This repository has been archived by the owner on Feb 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
如何用unit_test_db进行DAO单元测试
钱超 edited this page Jun 7, 2018
·
5 revisions
俗话说:没有单元测试的代码是耍流氓。写单元测试是每个程序员的必备节操。
简单的单元测试,junit,testng;复杂点的单元测试,推荐用jmockit;而DAO的单元测试,则是五花八门,或者嫌其复杂,很多都不去实现。
这里介绍下unit-test-db,它是从内部一个项目剥离出来的,并结合现有实现做了大量优化,纯粹做DAO的单元测试,下面介绍如何使用。
- unitils.properties(文件名不能修改):数据库表只需要建空表即可
unitils.modules=database,spring
database.type=mysql
database.url=jdbc:mysql://127.0.0.1:3306/test_db
database.userName=admin
database.password=123456
database.schemaNames=houyiregiondb
database.driverClassName=com.mysql.jdbc.Driver
#DatabaseModule.Transactional.value.default=disabled
DatabaseModule.Transactional.value.default=rollback
- spring-test-dao-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byName" >
<!--这一行是关键 -->
<bean id="regionDataSource" class="org.unitils.database.UnitilsDataSourceFactoryBean"/>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:sqlmap/sqlmap-config.xml"/>
<property name="dataSource" ref="regionDataSource"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="regionDataSource" />
</property>
</bean>
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" >
<property name="transactionManager" ref="transactionManager"/>
</bean>
<!-- 引入业务代码的DAO文件 -->
<import resource="classpath:spring/spring-dao-regiondb.xml" />
</beans>
- 基础类,后面的DAO测试,只需要继承这个类即可
@SpringApplicationContext("classpath:spring-test-dao-context.xml")
public abstract class BaseRegionDbDAOTestCase extends UnitilsJUnit4 implements ICore {
}
public class KeyValueConfigInfoDAOImplTest extends BaseRegionDbDAOTestCase {
private final String TABLE = "key_value_config_info";
@SpringBeanByName
private KeyValueConfigInfoDAO keyValueConfigInfoDAO;
@Test
public void testInsert() {
IDatabase.db.table(TABLE).clean();
KeyValueConfigInfoDO keyValueConfigInfoDO = new KeyValueConfigInfoDO();
keyValueConfigInfoDO.setConfigKey("best");
keyValueConfigInfoDO.setConfigValue("messi");
keyValueConfigInfoDAO.insert(keyValueConfigInfoDO);
IDatabase.db.table(TABLE).query().sizeEq(1).propertyEq("config_key", "best");
}
@Test
public void testUpdate() {
IDatabase.db.table(TABLE).clean().insert(2, new DataMap() {
{
this.put("config_key", "best1", "best");
this.put("config_value", "raw_value");
this.put("gmt_create", new Date());
this.put("gmt_modify", new Date());
}
});
KeyValueConfigInfoDO keyValueConfigInfoDO = new KeyValueConfigInfoDO();
keyValueConfigInfoDO.setConfigKey("best");
keyValueConfigInfoDO.setConfigValue("messi");
keyValueConfigInfoDAO.update(keyValueConfigInfoDO);
IDatabase.db.table(TABLE).query().sizeEq(2);
IDatabase.db.table(TABLE).queryWhere("config_key='best'").sizeEq(1).propertyEq("config_value", "messi");
IDatabase.db.table(TABLE).queryWhere("config_key='best1'").sizeEq(1).propertyEq("config_value", "raw_value");
}
}
<dependency>
<groupId>com.aliyun.test</groupId>
<artifactId>unit-test-db-datamap</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
- 方法2: