隨筆-55  評論-208  文章-0  trackbacks-0
          這兩天的花費了一些時間在使用spring-mock進行spring的單元測試問題上,基本上對這個問題有了一個完整的解決方案
          下面將對相關學習到和思考到的一些問題進行一下整理:
          1、首先明確一下問題:要對什么進行單元測試?
          我的問題是圍繞這一個DAO展開,我定義了一個DAO接口XXXDAO,并且寫了該DAO的實現XXXDAOImpl,然后使用spring對接口的實現進行管理:
          <bean id="XXXDAO" class="com.easou.datastat.base.dao.impl.XXXDAOImpl">
              
          <property name="sessionFactory">
                
          <ref bean="sessionFactory"/>
              
          </property>
              
          <property name="hibernateTemplate">
                
          <ref bean="hibernateTemplate"/>
              
          </property>
            
          </bean>

          現在問題開始明確了,我要對XXXDAOImpl進行單元測試。這里需要指明一個問題就是單元測試,測試的是有實現代碼的實現類,而不是接口。

          2、接著是下一個問題,如何取得Sping的上下文關系呢?
          這里就需要用到spring-mock了,其中有一個測試基類是AbstractTransactionalDataSourceSpringContextTests
          我讓測試用例XXXDAOImplTestcase繼承AbstractTransactionalDataSourceSpringContextTests,然后override其中的方法getConfigLocations()
          具體代碼如下:
          public class XXXDAOImplTestCase extends AbstractTransactionalDataSourceSpringContextTests {

              
              @Override
              
          protected String[] getConfigLocations() {
                  
          // TODO get the spring context
                  
                  String[] config 
          = new String[]{"file:D:\\javawork\\workspace\\datastat\\docroot\\WEB-INF\\applicationContext.xml","file:D:\\javawork\\workspace\\datastat\\docroot\\WEB-INF\\applicationContext-base.xml"};
                  
                  
          return config;

              }

          }

          不難看出,該方法是為了讀入spring的上下文信息,這樣的話這個測試用例就可以直接使用spirng中定義的bean了
          但是這里需要注意一個問題*,就是文件路徑的問題,這里為了能夠清楚地說明文件的路徑規則我引用了spring-mock官方的getConfigLocation方法說明
          Subclasses must implement this method to return the locations of their config files. A plain path will be treated as class path location. E.g.: "org/springframework/whatever/foo.xml". Note however that you may prefix path locations with standard Spring resource prefixes. Therefore, a config location path prefixed with "classpath:" with behave the same as a plain path, but a config location such as "file:/some/path/path/location/appContext.xml" will be treated as a filesystem location.

          3 好了,該到最后一個問題了,取得Spirng上下問中定義的bean的實例來進行單元測試
          這里有兩種方法,一種是定義一個代測單元的對象,然后定義好get,set方法,AbstractTransactionalDataSourceSpringContextTests會自動根據Spring的上下文得到待測單元的具體實現。
          private ExQueryDAO exQqueryDAO;
              
              
              
              
          /**
               * 
          @return 返回 exQqueryDAO。
               
          */

              
          public ExQueryDAO getExQqueryDAO() {
                  
          return exQqueryDAO;
              }



              
          /**
               * 
          @param exQqueryDAO 要設置的 exQqueryDAO。
               
          */

              
          public void setExQqueryDAO(ExQueryDAO exQqueryDAO) {
                  
          this.exQqueryDAO = exQqueryDAO;
              }


          另外一種就是使用applicationContext.getBean("XXXDAO");的方法取得實例
          XXXDAO xxxDAO = (XXXDAO)applicationContext.getBean("XXXDAO");

          接下來你就可以用這個bean的具體實現對相關方法進行unittest了



          這里補充一個問題就是單元測試的運行環境問題*,由于你可能在spring的配置文件中指定一些外部類所以你必須在junit的運行環境中指定使用到的classpath,下面是eclipse的junit配置classpath
          junit-classpath.JPG


          最后我對整體的spring+hibernate的jsf框架進行了一下單元測試的框架總結,主要多了一個基本的BaseTestCase,用于統一讀取applicationContext
          單元測試框架.jpg

          可以看到下面的pagebean和jsp頁面以及相關的taglib的單元測試我還沒有完成:),我將在今后幾天繼續研究相關的單元測試問題
          posted on 2006-08-25 00:29 rocket 閱讀(6244) 評論(4)  編輯  收藏

          評論:
          # re: Spring+hibernate 單元測試框架總結 2006-08-25 08:55 | sjun
          不錯,畫的測試圖把測試過程描述得很清晰,頂一下!  回復  更多評論
            
          # re: Spring+hibernate 單元測試框架總結[未登錄] 2007-06-13 20:08 | Jack
          rocket 支持你。  回復  更多評論
            
          # re: Spring+hibernate 單元測試框架總結 2007-09-25 11:43 | cocin0923@sina.com.cn
          不錯,圖畫的不錯,一下就知道是咋回事了  回復  更多評論
            
          # re: Spring+hibernate 單元測試框架總結 2007-11-17 15:25 | 汪保杰(Jack)
          管理與技術群
          47763528
          請快加入,要滿了!  回復  更多評論
            

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


          網站導航:
           
          主站蜘蛛池模板: 包头市| 云龙县| 北票市| 玉林市| 新宾| 池州市| 大洼县| 台江县| 滁州市| 潮州市| 马公市| 兴国县| 公安县| 丹阳市| 梁平县| 龙州县| 岗巴县| 白河县| 福贡县| 府谷县| 尼木县| 平远县| 渑池县| 临清市| 乌拉特后旗| 越西县| 耒阳市| 玉林市| 沛县| 岢岚县| 嘉黎县| 庆元县| 苍南县| 永善县| 兴化市| 灵宝市| 乐昌市| 凤庆县| 吐鲁番市| 乐平市| 石渠县|