Java中经常对service层和dao层进行单元测试,对dao层进行测试可以使用dbunit进行测试,对service可以使用easyMock和spring-test.jar进行测试
(1)dao层使用dbunit进行测试
①写一个关于Assert的类,这个类主要是用来使用junit进行实际与理想值的比较
import java.util.List; import junit.framework.Assert; public class EntitiesHelper { private static User baseUser = new User(1,"admin1"); public static void assertUser(User expected,User actual) { Assert.assertNotNull(expected); Assert.assertEquals(expected.getId(), actual.getId()); Assert.assertEquals(expected.getUsername(), actual.getUsername()); } public static void assertUsers(List<User> expected,List<User> actuals) { for(int i=0;i<expected.size();i++) { User eu = expected.get(i); User au = actuals.get(i); assertUser(eu, au); } } public static void assertUser(User expected) { assertUser(expected, baseUser); } }
②DBUtil这个类,主要是进行数据库的相关基本模拟操作
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DbUtil { public static Connection getConnection() throws SQLException { Connection con = null; con = DriverManager.getConnection("jdbc:mysql://localhost:3306/cms_test", "root", "123465"); return con; } public static void close(Connection con) { try { if(con!=null) con.close(); } catch (SQLException e) { e.printStackTrace(); } } public static void close(PreparedStatement ps) { try { if(ps!=null) ps.close(); } catch (SQLException e) { e.printStackTrace(); } } public static void close(ResultSet rs) { try { if(rs!=null) rs.close(); } catch (SQLException e) { e.printStackTrace(); } } }③AbstractDbUnitTestCase这个类就是DBunit这个工具的具体定义,内部通过上面(2)的DBUtil和下面(4)的xml文件模拟一个数据库的关系操作
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.sql.SQLException; import junit.framework.Assert; import org.dbunit.DatabaseUnitException; import org.dbunit.database.DatabaseConnection; import org.dbunit.database.IDatabaseConnection; import org.dbunit.database.QueryDataSet; import org.dbunit.dataset.DataSetException; import org.dbunit.dataset.IDataSet; import org.dbunit.dataset.xml.FlatXmlDataSet; import org.dbunit.dataset.xml.FlatXmlProducer; import org.dbunit.operation.DatabaseOperation; import org.junit.AfterClass; import org.junit.BeforeClass; import org.xml.sax.InputSource; public class AbstractDbUnitTestCase { public static IDatabaseConnection dbunitCon; private File tempFile; @BeforeClass public static void init() throws DatabaseUnitException, SQLException { dbunitCon = new DatabaseConnection(DbUtil.getConnection()); } protected IDataSet createDateSet(String tname) throws DataSetException { InputStream is = AbstractDbUnitTestCase .class .getClassLoader().getResourceAsStream(tname+".xml"); Assert.assertNotNull("dbunit的基本数据文件不存在",is); return new FlatXmlDataSet(new FlatXmlProducer(new InputSource(is))); } protected void backupAllTable() throws SQLException, IOException, DataSetException { IDataSet ds = dbunitCon.createDataSet(); writeBackupFile(ds); } private void writeBackupFile(IDataSet ds) throws IOException, DataSetException { tempFile = File.createTempFile("back","xml"); FlatXmlDataSet.write(ds, new FileWriter(tempFile)); } protected void backupCustomTable(String[] tname) throws DataSetException, IOException { QueryDataSet ds = new QueryDataSet(dbunitCon); for(String str:tname) { ds.addTable(str); } writeBackupFile(ds); } protected void bakcupOneTable(String tname) throws DataSetException, IOException { backupCustomTable(new String[]{tname}); } protected void resumeTable() throws FileNotFoundException, DatabaseUnitException, SQLException { IDataSet ds = new FlatXmlDataSet(new FlatXmlProducer(new InputSource(new FileInputStream(tempFile)))); DatabaseOperation.CLEAN_INSERT.execute(dbunitCon, ds); } @AfterClass public static void destory() { try { if(dbunitCon!=null) dbunitCon.close(); } catch (SQLException e) { e.printStackTrace(); } } }④xml配置文件,和数据库的字段是完全对应的,用来建立基本的本地数据库数据
<?xml version="1.0" encoding="UTF-8"?> <dataset> <t_user id="1" username="admin1"/> <t_user id="2" username="admin2"/> <t_user id="3" username="admin3"/> <t_user id="4" username="admin4"/> <t_user id="5" username="admin5"/> <t_user id="6" username="admin6"/> <t_user id="7" username="admin7"/> <t_user id="8" username="admin8"/> <t_user id="9" username="admin9"/> <t_user id="10" username="admin10"/> <t_user id="11" username="admin11"/> <t_user id="12" username="admin12"/> <t_user id="13" username="admin13"/> <t_user id="14" username="admin14"/> <t_user id="15" username="admin15"/> <t_user id="16" username="admin16"/> <t_user id="17" username="admin17"/> <t_user id="18" username="admin18"/> </dataset>⑤测试java代码
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.SQLException; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.inject.Inject; import org.dbunit.DatabaseUnitException; import org.dbunit.dataset.DataSetException; import org.dbunit.dataset.IDataSet; import org.dbunit.operation.DatabaseOperation; import org.hibernate.ObjectNotFoundException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.orm.hibernate4.SessionHolder; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; import org.springframework.transaction.support.TransactionSynchronizationManager; import com.github.springtestdbunit.DbUnitTestExecutionListener; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("/beans.xml") @TestExecutionListeners({DbUnitTestExecutionListener.class, DependencyInjectionTestExecutionListener.class}) public class TestUserDao extends AbstractDbUnitTestCase{ @Inject private SessionFactory sessionFactory; @Inject private IUserDao userDao; @Before public void setUp() throws DataSetException, SQLException, IOException { Session s = sessionFactory.openSession(); TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(s)); this.backupAllTable(); } @Test public void testLoad() throws DatabaseUnitException, SQLException { IDataSet ds = createDateSet("t_user"); DatabaseOperation.CLEAN_INSERT.execute(dbunitCon,ds); User u = userDao.load(1); EntitiesHelper.assertUser(u); } @Test(expected=ObjectNotFoundException.class) public void testDelete() throws DatabaseUnitException, SQLException { IDataSet ds = createDateSet("t_user"); DatabaseOperation.CLEAN_INSERT.execute(dbunitCon,ds); userDao.delete(1); User tu = userDao.load(1); System.out.println(tu.getUsername()); } @Test public void testListByArgs() throws DatabaseUnitException, SQLException { IDataSet ds = createDateSet("t_user"); DatabaseOperation.CLEAN_INSERT.execute(dbunitCon,ds); SystemContext.setOrder("desc"); SystemContext.setSort("id"); List<User> expected = userDao.list("from User where id>? and id<?", new Object[]{1,4}); List<User> actuals = Arrays.asList(new User(3,"admin3"),new User(2,"admin2")); assertNotNull(expected); assertTrue(expected.size()==2); EntitiesHelper.assertUsers(expected, actuals); } @Test public void testListByArgsAndAlias() throws DatabaseUnitException, SQLException { IDataSet ds = createDateSet("t_user"); DatabaseOperation.CLEAN_INSERT.execute(dbunitCon,ds); SystemContext.setOrder("asc"); SystemContext.setSort("id"); Map<String,Object> alias = new HashMap<String,Object>(); alias.put("ids", Arrays.asList(1,2,3,5,6,7,8,9,10)); List<User> expected = userDao.list("from User where id>? and id<? and id in(:ids)", new Object[]{1,5},alias); List<User> actuals = Arrays.asList(new User(2,"admin2"),new User(3,"admin3")); assertNotNull(expected); assertTrue(expected.size()==2); EntitiesHelper.assertUsers(expected, actuals); } }(2)使用easymock对service进行测试
①增加easymock以及spring-test这两个用来测试的jar依赖
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <dependency> <groupId>org.easymock</groupId> <artifactId>easymock</artifactId> <version>3.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency>②需要在/src/test/resources/下增加一个xml文件,为test-beans.xml,可以直接拷贝前面spring用到的beans.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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!-- 打开Spring的Annotation支持 --> <context:annotation-config /> <bean id="userDao" class="org.easymock.EasyMock" factory-method="createStrictMock"> <constructor-arg value="org.lxp.cms.dao.IUserDao"/> </bean> <bean id="roleDao" class="org.easymock.EasyMock" factory-method="createStrictMock"> <constructor-arg value="org.lxp.cms.dao.IRoleDao"/> </bean> <bean id="groupDao" class="org.easymock.EasyMock" factory-method="createStrictMock"> <constructor-arg value="org.lxp.cms.dao.IGroupDao"/> </bean> <bean id="userService" class="org.lxp.cms.service.UserService"> <property name="userDao" ref="userDao"/> <property name="groupDao" ref="groupDao"/> <property name="roleDao" ref="roleDao"/> </bean> <bean id="groupService" class="org.lxp.cms.service.GroupService"> <property name="userDao" ref="userDao"/> <property name="groupDao" ref="groupDao"/> </bean> <bean id="roleService" class="org.lxp.cms.service.RoleService"> <property name="userDao" ref="userDao"/> <property name="roleDao" ref="roleDao"/> </bean> </beans>
③测试java代码实现
import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.expectLastCall; import static org.easymock.EasyMock.replay; import static org.easymock.EasyMock.reset; import static org.easymock.EasyMock.verify; import java.util.Arrays; import java.util.List; import javax.inject.Inject; import org.easymock.EasyMock; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("/test-beans.xml") public class TestUserService { @Inject private IUserService userService; @Inject private IRoleDao roleDao; @Inject private IUserDao userDao; @Inject private IGroupDao groupDao; private User baseUser = new User(1,"admin1","123","admin1","admin1@","123",0); @Test public void testDelete() { reset(userDao); int uid = 2; userDao.deleteUserGroups(uid); expectLastCall(); userDao.deleteUserRoles(uid); expectLastCall(); userDao.delete(uid); expectLastCall(); replay(userDao); userService.delete(uid); verify(userDao); } @Test public void testUpdateStatus() { reset(userDao); int uid = 2; expect(userDao.load(uid)).andReturn(baseUser); userDao.update(baseUser); expectLastCall(); replay(userDao); userService.updateStatus(uid); Assert.assertEquals(baseUser.getStatus(), 1); verify(userDao); } @Test(expected=CmsException.class) public void testUpdateStatusNoUser() { reset(userDao); int uid = 2; expect(userDao.load(uid)).andReturn(null); userDao.update(baseUser); expectLastCall(); replay(userDao); userService.updateStatus(uid); Assert.assertEquals(baseUser.getStatus(), 1); verify(userDao); } @Test public void testFindUser() { reset(userDao); expect(userDao.findUser()).andReturn(new Pager<User>()); replay(userDao); userService.findUser(); verify(userDao); } }