使用unitils的dbunit模塊進行測試。 java
真正的去訪問數據庫,每次測試以前打開一個事務,插入測試數據,業務操做,斷言測試數據,回滾。 mysql
其中unitils+dbunit實現了除業務操做以外的全部步驟。 sql
dbunit提供了將xml直接轉換爲數據庫數據的功能。 數據庫
unitils則進一步封裝,提供了@DataSet,@ExpectDataset以及事務管理等功能,能夠經過annotation的方式將數據文件導入數據庫,也能夠經過xml去斷言數據,而且支持自動回滾,超級方便。 測試
這裏在使用unitils整合dbunit的時候碰到了兩個問題,記錄一下。關於問題,請分別查看下面兩篇文章: spa
unitils使用@DataSet插入測試數據,測試結束後不能回滾 .net
mysql+unitils用@DataSet,拋NoSuchColumnException code
最後,貼上一段標準DAO測試的代碼: xml
@DataSet public final class RoleDaoTest extends IntegrateBaseTest { @SpringBeanByType private RoleDao roleDao; @Test @ExpectedDataSet public void save() { Role role = new Role(); role.setName("Test Role"); role.assignPermissions(Lists.newArrayList(RbacPermission.AssignRolePermission)); role.denyPermissions(Lists.newArrayList(RbacPermission.DenyRolePermission)); roleDao.save(role); } @Test @ExpectedDataSet public void delete() { roleDao.delete(1L); } @Test public void findOne() { assertReflectionEquals(roleDao.findOne(1L), createFristRole()); assertReflectionEquals(roleDao.findOne(2L), createSecondRole()); assertReflectionEquals(roleDao.findOne(3L), createThirdRole()); } @Test public void findAll() { Page<Role> roles = roleDao.findAll(new PageRequest(0, 10)); assertThat(roles.getTotalElements(), is(3L)); assertThat(roles.getTotalPages(), is(1)); assertThat(roles.getContent().size(), is(3)); assertReflectionEquals(roles.getContent().get(0), createFristRole()); assertReflectionEquals(roles.getContent().get(1), createSecondRole()); assertReflectionEquals(roles.getContent().get(2), createThirdRole()); roles = roleDao.findAll(new PageRequest(0, 2)); assertThat(roles.getTotalElements(), is(3L)); assertThat(roles.getTotalPages(), is(2)); assertThat(roles.getContent().size(), is(2)); assertReflectionEquals(roles.getContent().get(0), createFristRole()); assertReflectionEquals(roles.getContent().get(1), createSecondRole()); roles = roleDao.findAll(new PageRequest(1, 2, new Sort(new Order(Direction.DESC, "id")))); assertThat(roles.getTotalElements(), is(3L)); assertThat(roles.getTotalPages(), is(2)); assertThat(roles.getContent().size(), is(1)); assertReflectionEquals(roles.getContent().get(0), createFristRole()); } private Role createFristRole() { Role result = new Role(); result.setId(1L); result.setName("Super Admin"); result.setVersion(0L); AssignedRolePermission assignedRolePermission = new AssignedRolePermission(); assignedRolePermission.setId(1L); assignedRolePermission.setPermissionName("com.miraclesea.module.rbac.vo.RbacPermission#AssignRolePermission"); assignedRolePermission.setVersion(0L); assignedRolePermission.setRole(result); result.setAssignedPermissions(Lists.newArrayList(assignedRolePermission)); DeniedRolePermission deniedRolePermission = new DeniedRolePermission(); deniedRolePermission.setId(2L); deniedRolePermission.setPermissionName("com.miraclesea.module.rbac.vo.RbacPermission#DenyRolePermission"); deniedRolePermission.setVersion(0L); deniedRolePermission.setRole(result); result.setDeniedPermissions(Lists.newArrayList(deniedRolePermission)); return result; } private Role createSecondRole() { Role result = new Role(); result.setId(2L); result.setName("Admin"); result.setVersion(0L); AssignedRolePermission assignedRolePermission = new AssignedRolePermission(); assignedRolePermission.setId(3L); assignedRolePermission.setPermissionName("com.miraclesea.module.rbac.vo.RbacPermission#AssignRolePermission"); assignedRolePermission.setVersion(0L); assignedRolePermission.setRole(result); result.setAssignedPermissions(Lists.newArrayList(assignedRolePermission)); return result; } private Role createThirdRole() { Role result = new Role(); result.setId(3L); result.setName("User"); result.setVersion(0L); return result; } }