Energy of Love  
          日歷
          <2012年10月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910
          統計
          • 隨筆 - 70
          • 文章 - 0
          • 評論 - 80
          • 引用 - 0

          導航

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

           
          使用Junit4.4測試 
          在類上的配置Annotation 
          @RunWith(SpringJUnit4ClassRunner.class) 用于配置spring中測試的環境 
          @ContextConfiguration(Locations="../applicationContext.xml") 用于指定配置文件所在的位置 
          @Test標注在方法前,表示其是一個測試的方法 無需在其配置文件中額外設置屬性. 

          多個配置文件時{"/applic","/asas"} 可以導入多個配置文件 

          測試中的事務配置 , 
          AbstractTransactionalJUnit38SpringContextTests、 AbstractTransactionalJUnit4SpringContextTests 
          AbstractTransactionalTestNGSpringContextTests 
          已經在類級別預先配置了好了事物支持 

          在普通spring的junit環境中配置事務 
          在類之前加入注解 
          @TransactionConfiguration(transactionManagert="txMgr",defaultRollback=false) 
          @Transactional 
          在方法中主要使用的Annotation包括 
          @TestExecutionListeners({})---用于禁用默認的監聽器 否著需要通過@contextconfiguration配置一個ApplicationContext; 

          @BeforeTransaction 
          @Before 
          @Rollback(true) 
          @AfterTransaction 
          @NotTransactional 

          Junit4.4下支持類,方便基于junit4.4的測試 
          AbstractJUnit4SpringContextTests: 

          AbstractTransactionalJUnit4SpringContextTests: 
          需要在applicationContext中定義一個datasource 

          2009年3月9日 
          目前Spring2.5只支持4.4的Junit進行測試 
          下面是一個簡單的測試Demo 

           1 package com.gameplus.service.webService;
           2 
           3 import javax.annotation.Resource;
           4 
           5 import org.junit.Test;
           6 import org.junit.runner.RunWith;
           7 import org.springframework.test.context.ContextConfiguration;
           8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
           9 
          10 @RunWith(SpringJUnit4ClassRunner.class)
          11 @ContextConfiguration(locations={"../../../../applicationContext.xml","../../../../applicationDatasource.xml"})
          12 public class UserServiceTest  {
          13     @Resource
          14     private IUserService userService;
          15     
          16     @Test
          17     public void testAddOpinion1() {
          18         userService.downloadCount(1);
          19         System.out.println(1);
          20     }
          21     @Test
          22     public void testAddOpinion2() {
          23         userService.downloadCount(2);
          24         System.out.println(2);
          25     }
          26 }
          27 

          注意需要新的Jar包如下 
          javassist-3.4.GA.jar 
          hibernate3.jar 
          hibernate-annotations.jar 
          尤其注意用新版的,舊版會出現類未找到的異常 
          posted on 2011-11-30 19:38 不高興 閱讀(54740) 評論(3)  編輯  收藏 所屬分類: Java
          評論:
           
          Copyright © 不高興 Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 宁安市| 盘山县| 黄浦区| 根河市| 门头沟区| 凉城县| 肇东市| 隆德县| 海安县| 乌海市| 扬州市| 鄂托克前旗| 商水县| 武鸣县| 九龙县| 永昌县| 孝昌县| 汝南县| 利辛县| 和政县| 都江堰市| 宜阳县| 密山市| 且末县| 广元市| 砀山县| 巨鹿县| 南丹县| 黄山市| 佛坪县| 寻乌县| 南投市| 余姚市| 桂阳县| 吉木萨尔县| 肥东县| 敦化市| 全椒县| 武义县| 新巴尔虎右旗| 中西区|