Energy of Love  
          日歷
          <2011年11月>
          303112345
          6789101112
          13141516171819
          20212223242526
          27282930123
          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 不高興 閱讀(54739) 評論(3)  編輯  收藏 所屬分類: Java
          評論:
           
          Copyright © 不高興 Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 东乡族自治县| 望江县| 华阴市| 四平市| 达日县| 闻喜县| 祁东县| 公安县| 贡嘎县| 当雄县| 峨眉山市| 自治县| 安仁县| 柘城县| 大连市| 湾仔区| 呈贡县| 常山县| 巴楚县| 昆山市| 会宁县| 鹤岗市| 遂宁市| 多伦县| 汝南县| 钟山县| 星座| 呼玛县| 南丹县| 自贡市| 云阳县| 天柱县| 扶风县| 板桥市| 青龙| 腾冲县| 台前县| 邳州市| 锦州市| 伊通| 崇阳县|