使用unitils的dbunit模块进行测试。
真正的去访问数据库,每次测试之前打开一个事务,插入测试数据,业务操作,断言测试数据,回滚。
其中unitils+dbunit实现了除业务操作之外的所有步骤。
dbunit提供了将xml直接转换为数据库数据的功能。
unitils则进一步封装,提供了@DataSet,@ExpectDataset以及事务管理等功能,可以通过annotation的方式将数据文件导入数据库,也可以通过xml去断言数据,并且支持自动回滚,超级方便。
这里在使用unitils整合dbunit的时候碰到了两个问题,记录一下。关于问题,请分别查看下面两篇文章:
unitils使用@DataSet插入测试数据,测试结束后不能回滚
mysql+unitils用@DataSet,抛NoSuchColumnException
最后,贴上一段标准DAO测试的代码:
@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;
}
}
来源:oschina
链接:https://my.oschina.net/u/719192/blog/173647