分享

SSH的Service层做单元测试

 X的世界 2012-08-03
选用框架为Spring2.0 + Hibernate 3.2 + Struts2.0 + Junit4.7

1、首先用一个基础测试类继承Spring的AbstractTransactionalDataSourceSpringContextTests类,在getConfigLocations方法中申明配置文件的存放路径。
Java代码 复制代码 收藏代码
  1. package service;   
  2.   
  3. import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;   
  4.   
  5. public class BaseServiceTest extends  
  6.         AbstractTransactionalDataSourceSpringContextTests {   
  7.   
  8.     protected String[] getConfigLocations() {   
  9.         setAutowireMode(AUTOWIRE_BY_TYPE);   
  10.         return new String[] { "classpath*:cfg/spring/*.xml",   
  11.                 "file:WebRoot/WEB-INF/applicationContext.xml" };   
  12.     }   
  13.   
  14. }  


2、具体的Service单元测试如下:
Java代码 复制代码 收藏代码
  1. package service;   
  2.   
  3. import java.util.List;   
  4.   
  5. import org.junit.Test;   
  6.   
  7. import com.ys.system.dept.bean.SysDept;   
  8. import com.ys.system.dept.service.IDeptService;   
  9.   
  10. public class DeptServiceTest extends BaseServiceTest {   
  11.   
  12.     private final static int UNIT_ID = 79// magic number   
  13.     private static int DEPT_ID;   
  14.   
  15.     private IDeptService deptService;   
  16.   
  17.     public void setDeptService(IDeptService deptService) {   
  18.         this.deptService = deptService;   
  19.     }   
  20.   
  21.     @Test  
  22.     public void testDeptsOfList() {   
  23.         System.out.println("\n");   
  24.         List<SysDept> depts = deptService.findDeptsOfUnit(UNIT_ID);   
  25.         for (SysDept sysDept : depts) {   
  26.             System.out.println(sysDept);   
  27.         }   
  28.     }   
  29.   
  30.     @Test  
  31.     public void testChildDepts() {   
  32.         System.out.println("\n");   
  33.         List<SysDept> depts = deptService.findChildDepts(0);   
  34.         for (SysDept sysDept : depts) {   
  35.             System.out.println(sysDept);   
  36.         }   
  37.     }   
  38.   
  39.     @Test  
  40.     public void testSaveAndUpdate() {   
  41.         System.out.println("\n");   
  42.         SysDept dept = new SysDept();   
  43.         dept.setDeptName("service_test");   
  44.         dept.setDeptDescription("测试");   
  45.         dept.setParentDeptId(0);   
  46.         dept.setUnitId(UNIT_ID);   
  47.         deptService.save(dept);   
  48.   
  49.         // 如果不写下句,HIBERNATE不会帮你做实际的数据库操作的,奸诈啊   
  50.         setComplete();   
  51.         assertNotNull(dept.getId());   
  52.         DEPT_ID = dept.getId();   
  53.   
  54.         dept.setDeptDescription("修改部门备注");   
  55.         deptService.save(dept);   
  56.         setComplete();   
  57.         assertEquals(dept.getDeptDescription(), "修改部门备注");   
  58.     }   
  59.   
  60.     @Test  
  61.     public void testFindByIdAndDelete() {   
  62.         System.out.println("\n");   
  63.         SysDept dept = deptService.findById(DEPT_ID);   
  64.         assertNotNull(dept.getDeptName());   
  65.         System.out.println(dept.getDeptName());   
  66.   
  67.         deptService.delete(dept);   
  68.         setComplete();   
  69.   
  70.         dept = deptService.findById(DEPT_ID);   
  71.         assertNull(dept);   
  72.     }   
  73.   
  74. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多