mybatis是一款很不错的ORM框架,在项目中得到了很多应用。为了规范单元测试的习惯,特地基于Junit4为DAO层写了测试用例。基础测试从单表开始,数据表基本DDL为:
Create TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userName` varchar(50) DEFAULT NULL,
`userAge` int(11) DEFAULT NULL,
`userAddress` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`)
)
在实际写DAO层单元测试用例时,工程仅需要hamcrest-core,junit-4,mybatis,数据库驱动这四个jar包。创建基础Bean后,书写mybatis启动的核心配置文件config.xml:
/p>
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
DAO接口及实现部分代码:
package com.dao;
import com.model.User;
public interface UserDao {
public User selectUserByID(int id);
public int deleteById(int id);
public int insertOne(User user);
public int updateOne(User user);
}
package com.dao.impl;
import org.apache.ibatis.session.SqlSession;
import com.base.SqlSessionBase;
import com.dao.UserDao;
import com.model.User;
public class UserDaoImpl extends SqlSessionBase implements UserDao{
private SqlSession session;
UserDao ud;
@Override
public User selectUserByID(int id) {
try{
session = SqlSessionBase.getSqlSession();
ud = session.getMapper(UserDao.class);
return ud.selectUserByID(id);
} finally{
session.close();
}
}
@Override
public int deleteById(int id) {
try{
session = SqlSessionBase.getSqlSession();
ud = session.getMapper(UserDao.class);
return ud.deleteById(id);
} finally{
session.commit();
session.close();
}
}
@Override
public int insertOne(User user) {
try{
session = SqlSessionBase.getSqlSession();
ud = session.getMapper(UserDao.class);
return ud.insertOne(user);
} finally{
session.commit();
session.close();
}
}
@Override
public int updateOne(User user) {
try{
session = SqlSessionBase.getSqlSession();
ud = session.getMapper(UserDao.class);
return ud.updateOne(user);
} finally{
session.commit();
session.close();
}
}
}
工具类:
package com.base;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class SqlSessionBase {
private static SqlSessionFactory sqlSessionFactory;
private static Reader reader;
public static SqlSession getSqlSession() {
try {
reader = Resources.getResourceAsReader("config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (Exception e) {
e.printStackTrace();
}
return sqlSessionFactory.openSession();
}
}
随后,使用Junit4书写单元测试用例:
package test;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import com.dao.impl.UserDaoImpl;
import com.model.User;
@RunWith(BlockJUnit4ClassRunner.class)
public class TestUserDao {
private UserDaoImpl ud = new UserDaoImpl();
@Test
public void testSelectOne(){
Assert.assertEquals(ud.selectUserByID(1).getClass(), User.class);
}
@Test
public void testDeleteOne(){
Assert.assertEquals(ud.deleteById(2), 1);
}
@Test
public void testInsertone(){
User user= new User();
user.setId(2);
user.setUserAddress("Somewhere");
Assert.assertEquals(ud.insertOne(user), 1);
}
@Test
public void testUpdateOne(){
User user= new User();
user.setId(1);
user.setUserAddress("China xian");
user.setUserName("New name");
Assert.assertEquals(ud.updateOne(user), 1);
}
}
由于是对DAO层的测试,因此使用的断言较为简洁。Junit4提供的Assert API借用了hamcrest提供的断言语法AsserThat,在字符匹配、集合匹配等方面有了更直观的断言表达。在写单元测试用例中,正确使用断言语法尤为重要。