Energy of Love  
          日歷
          <2012年10月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910
          統(tǒng)計(jì)
          • 隨筆 - 70
          • 文章 - 0
          • 評(píng)論 - 80
          • 引用 - 0

          導(dǎo)航

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

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

          多個(gè)配置文件時(shí){"/applic","/asas"} 可以導(dǎo)入多個(gè)配置文件 

          測(cè)試中的事務(wù)配置 , 
          AbstractTransactionalJUnit38SpringContextTests、 AbstractTransactionalJUnit4SpringContextTests 
          AbstractTransactionalTestNGSpringContextTests 
          已經(jīng)在類級(jí)別預(yù)先配置了好了事物支持 

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

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

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

          AbstractTransactionalJUnit4SpringContextTests: 
          需要在applicationContext中定義一個(gè)datasource 

          2009年3月9日 
          目前Spring2.5只支持4.4的Junit進(jìn)行測(cè)試 
          下面是一個(gè)簡(jiǎn)單的測(cè)試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 
          尤其注意用新版的,舊版會(huì)出現(xiàn)類未找到的異常 
          posted on 2011-11-30 19:38 不高興 閱讀(54740) 評(píng)論(3)  編輯  收藏 所屬分類: Java
          評(píng)論:
           
          Copyright © 不高興 Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 永仁县| 建阳市| 台江县| 娱乐| 大渡口区| 龙泉市| 曲沃县| 靖边县| 明光市| 呼玛县| 永兴县| 慈利县| 香格里拉县| 隆昌县| 宝兴县| 北海市| 滕州市| 临朐县| 佳木斯市| 怀安县| 五指山市| 方城县| 闽侯县| 昌图县| 汉阴县| 衡山县| 滦南县| 兴山县| 邯郸市| 郓城县| 平罗县| 樟树市| 会理县| 河西区| 阜南县| 枣庄市| 正阳县| 蕉岭县| 巴南区| 保靖县| 拉孜县|