qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Junit結合Spring對Dao層進行單元測試

           實際開發過程中,寫單元測試是非常難的一件事情,其主要原因是代碼結構不夠好,導致單元測試不好寫。特別是Dao層,因為Dao層代碼都是與數據庫相關的,所以我們在對Dao層代碼進行單元測試的時候,不僅僅要考慮我在上篇文章中提到的代碼隔離,還要注意單元測試不能帶來臟數據。另外,dao層實例依賴spring上下文,我們怎么樣來解決這個問題?

            看看下面的一個的測試實例:

          /**
          * @author lisanlai <br />
          * Mail: sanlai_lee@qq.com<br />
          * Blog:http://blog.lisanlai.cn <br />
          */
          @RunWith(SpringJUnit4ClassRunner.class)
          @ContextConfiguration({ "/META-INF/spring/sellmanager-context.xml",
          "/META-INF/spring/dao-context.xml",
          "/META-INF/spring/mvc-context.xml" })
          //@Transactional
          public class SysEmployeeDaoTest {
          /**
          * 測試deleteEmployee方法 .
          * Method Name:deleteEmployee .
          * the return type:void
          */
          @Test
          public void deleteEmployee() {
          Employee employee = new Employee();
          employee.setEmployeeCode(""+new Date().getTime());
          employee.setEmployeeName("lisanlai");
          employee.setDelFlag("0");
          String empId = sysEmployeeDao.save(employee);
          Assert.assertNotNull("新增的員工ID為null",empId);
          //把該id對應的員工刪除
          sysEmployeeDao.deleteEmployee(empId);
          //再用該ID去查數據庫,如果為空,說明刪除方法邏輯正確
          Employee emp = sysEmployeeDao.get(empId);
          Assert.assertNotNull(emp);
          Assert.assertArrayEquals("deleteEmployee方法邏輯不正確,員工沒有被刪除",
          new String[]{"1"}, new String[]{emp.getDelFlag()});
          //刪除員工對象
          sysEmployeeDao.delete(emp);
          }
          /**
          * 測試saveEmployee方法 .
          * Method Name:saveEmployee .
          * the return type:void
          */
          @Test
          @Transactional
          @Rollback(true)
          public void saveEmployee() {
          Employee employee = new Employee();
          employee.setEmployeeName("lisanlai");
          String empCode = ""+new Date().getTime();
          employee.setEmployeeCode(empCode);
          sysEmployeeDao.saveEmployee(employee);
          //通過code查找員工
          List<Employee> emps = sysEmployeeDao.findByNamedParam(
          new String[]{"employeeCode"},
          new String[]{empCode});
          Assert.assertTrue("saveEmployee方法邏輯錯誤,員工保存失敗!", !emps.isEmpty());
          }
          }


          注意類上的三個注解:

          //指定測試用例的運行器 這里是指定了Junit4
          @RunWith(SpringJUnit4ClassRunner.class)
          //指定Spring的配置文件 路徑相對classpath而言
          @ContextConfiguration({ "/META-INF/spring/sellmanager-context.xml",
          "/META-INF/spring/dao-context.xml",
          "/META-INF/spring/mvc-context.xml" })
          //如果在類上面使用該注解,這樣所有的測試方案都會自動的 rollback
          //@Transactional
          再注意saveEmployee方法上的兩個注解:
          //這個注解表示使用事務
          @Transactional
          //這個表示方法執行完以后回滾事務,如果設置為false,則不回滾
          @Rollback(true)



          posted on 2013-09-05 10:35 順其自然EVO 閱讀(4331) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2013年9月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 剑阁县| 巴塘县| 霍州市| 策勒县| 河北区| 平原县| 瑞金市| 工布江达县| 乾安县| 湛江市| 阳高县| 安吉县| 赣州市| 泰来县| 策勒县| 沿河| 阳高县| 青海省| 颍上县| 长寿区| 江永县| 鹤庆县| 万山特区| 曲靖市| 许昌县| 抚顺县| 平罗县| 泰宁县| 棋牌| 浠水县| 宁城县| 安岳县| 三明市| 边坝县| 沽源县| 沾化县| 靖西县| 古浪县| 霍州市| 兰西县| 泰宁县|