junit單元測(cè)試_第1頁(yè)
junit單元測(cè)試_第2頁(yè)
junit單元測(cè)試_第3頁(yè)
junit單元測(cè)試_第4頁(yè)
junit單元測(cè)試_第5頁(yè)
已閱讀5頁(yè),還剩14頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、1、建立Junit測(cè)試類1.1 右擊test測(cè)試包,選擇New->Oher. 2.2 在窗口中找到Junit,選擇Junit Test Case3.3 輸入名稱(Name),命名規(guī)則一般建議采用:類名+Test。Browse.選擇要測(cè)試的類,這里是StudentService。4.4 勾選要測(cè)試的方法5.5 生成后,效果如下 這里import static是引入Assert類中靜態(tài)屬性或靜態(tài)方法的寫(xiě)法。原來(lái)要Assert.fail(),現(xiàn)在只需直接fail()即可,即省略了Assert。 其實(shí)不通過(guò)Junit新建向?qū)?lái)建立也可以,隨便建立一個(gè)新類后,只需在方法上加入Test注解

2、即可。2、核心斷言斷言是編寫(xiě)測(cè)試用例的核心實(shí)現(xiàn)方式,即期望值是多少,測(cè)試的結(jié)果是多少,以此來(lái)判斷測(cè)試是否通過(guò)。2.1 斷言核心方法assertArrayEquals(expecteds, actuals)查看兩個(gè)數(shù)組是否相等。assertEquals(expected, actual)查看兩個(gè)對(duì)象是否相等。類似于字符串比較使用的equals()方法assertNotEquals(first, second)查看兩個(gè)對(duì)象是否不相等。assertNull(object)查看對(duì)象是否為空。assertNotNull(object)查看對(duì)象是否不為空。assertSame(expected, actu

3、al)查看兩個(gè)對(duì)象的引用是否相等。類似于使用“=”比較兩個(gè)對(duì)象assertNotSame(unexpected, actual)查看兩個(gè)對(duì)象的引用是否不相等。類似于使用“!=”比較兩個(gè)對(duì)象assertTrue(condition)查看運(yùn)行結(jié)果是否為true。assertFalse(condition)查看運(yùn)行結(jié)果是否為false。assertThat(actual, matcher)查看實(shí)際值是否滿足指定的條件fail()讓測(cè)試失敗2.2 示例1. package test;  2.   3. import static o

4、rg.hamcrest.CoreMatchers.*;  4. import static org.junit.Assert.*;  5.   6. import java.util.Arrays;  7.   8. import org.hamcrest.core.CombinableMatcher;  9. import org.junit.Test;  10.   11. publ

5、ic class AssertTests   12.   13.       Test  14.       public void testAssertArrayEquals()   15.         byte expected =

6、 "trial".getBytes();  16.         byte actual = "trial".getBytes();  17.         org.junit.Assert.assertArrayEquals("failure - byte arrays&#

7、160;not same", expected, actual);  18.         19.   20.       Test  21.       public void testAssertEquals()   22.  

8、60;      org.junit.Assert.assertEquals("failure - strings not same", 5l, 5l);  23.         24.   25.       Test  26.   &

9、#160;   public void testAssertFalse()   27.         org.junit.Assert.assertFalse("failure - should be false", false);  28.         29.

10、  30.       Test  31.       public void testAssertNotNull()   32.         org.junit.Assert.assertNotNull("should not be null",

11、0;new Object();  33.         34.   35.       Test  36.       public void testAssertNotSame()   37.        

12、; org.junit.Assert.assertNotSame("should not be same Object", new Object(), new Object();  38.         39.   40.       Test  41.   

13、0;   public void testAssertNull()   42.         org.junit.Assert.assertNull("should be null", null);  43.         44.   45.   

14、;    Test  46.       public void testAssertSame()   47.         Integer aNumber = Integer.valueOf(768);  48.        

15、; org.junit.Assert.assertSame("should be same", aNumber, aNumber);  49.         50.   51.       / JUnit Matchers assertThat  52.    

16、60;  Test  53.       public void testAssertThatBothContainsString()   54.         org.junit.Assert.assertThat("albumen", both(containsString("a").and(containsSt

17、ring("b");  55.         56.   57.       Test  58.       public void testAssertThathasItemsContainsString()   59.    

18、0;    org.junit.Assert.assertThat(Arrays.asList("one", "two", "three"), hasItems("one", "three");  60.         61.   62.      

19、; Test  63.       public void testAssertThatEveryItemContainsString()   64.         org.junit.Assert.assertThat(Arrays.asList(new String  "fun", "ban"

20、, "net" ), everyItem(containsString("n");  65.         66.   67.       / Core Hamcrest Matchers with assertThat  68.     

21、;  Test  69.       public void testAssertThatHamcrestCoreMatchers()   70.         assertThat("good", allOf(equalTo("good"), startsWith("good");&#

22、160; 71.         assertThat("good", not(allOf(equalTo("bad"), equalTo("good");  72.         assertThat("good", anyOf(equalTo("bad"), eq

23、ualTo("good");  73.         assertThat(7, not(CombinableMatcher.<Integer> either(equalTo(3).or(equalTo(4);  74.         assertThat(new Object(), not(sameInstance(ne

24、w Object();  75.         76.   3、核心注解3.1 說(shuō)明Before初始化方法After釋放資源Test測(cè)試方法,在這里可以測(cè)試期望異常和超時(shí)時(shí)間Ignore忽略的測(cè)試方法BeforeClass針對(duì)所有測(cè)試,只執(zhí)行一次,且必須為static voidAfterClass針對(duì)所有測(cè)試,只執(zhí)行一次,且必須為static voidRunWith指定測(cè)試類使用某個(gè)運(yùn)行器Parameters指定測(cè)試類的測(cè)試數(shù)據(jù)集合Rule允許靈活添加或重新定義測(cè)

25、試類中的每個(gè)測(cè)試方法的行為FixMethodOrder指定測(cè)試方法的執(zhí)行順序3.2 執(zhí)行順序 一個(gè)測(cè)試類單元測(cè)試的執(zhí)行順序?yàn)椋?BeforeClass > Before > Test > After > AfterClass 每一個(gè)測(cè)試方法的調(diào)用順序?yàn)椋?Before > Test > After3.3 示例1. package test;  2.   3. import static org.junit.Assert.*;  4.   5. imp

26、ort org.junit.*;  6.   7. public class JDemoTest   8.   9.     BeforeClass  10.     public static void setUpBeforeClass() throws Exception   11.  

27、;       System.out.println("in BeforeClass=");  12.       13.   14.     AfterClass  15.     public static void tearDownAfterClass()

28、0;throws Exception   16.         System.out.println("in AfterClass=");  17.       18.   19.     Before  20.     public v

29、oid before()   21.         System.out.println("in Before");  22.       23.   24.     After  25.     public void aft

30、er()   26.         System.out.println("in After");  27.       28.   29.     Test(timeout = 10000)  30.     public v

31、oid testadd()   31.         JDemo a = new JDemo();  32.         assertEquals(6, a.add(3, 3);  33.         Syst

32、em.out.println("in Test -Add");  34.       35.   36.     Test  37.     public void testdivision()   38.         JDe

33、mo a = new JDemo();  39.         assertEquals(3, a.division(6, 2);  40.         System.out.println("in Test -Division");  41.   &#

34、160;   42.   43.     Ignore  44.     Test  45.     public void test_ignore()   46.         JDemo a = new JDemo

35、();  47.         assertEquals(6, a.add(1, 5);  48.         System.out.println("in test_ignore");  49.       50.   51.  &

36、#160;  Test  52.     public void teest_fail()   53.         fail();  54.       55.   56.   57. class JDemo extends Threa

37、d   58.   59.     int result;  60.   61.     public int add(int a, int b)   62.         try   63.    

38、;         sleep(1000);  64.             result = a + b;  65.          catch (InterruptedException e)

39、   66.           67.         return result;  68.       69.   70.     public int division(int a, int&

40、#160;b)   71.         return result = a / b;  72.       73.   執(zhí)行結(jié)果:1. in BeforeClass=  2. in Before  3. in Test -Add  4.

41、 in After  5. in Before  6. in Test -Division  7. in After  8. in AfterClass=    圖中左上紅框中部分表示Junit運(yùn)行結(jié)果,5個(gè)成功(1個(gè)忽略),1個(gè)錯(cuò)誤,1個(gè)失敗。(注意錯(cuò)誤和失敗不是一回事,錯(cuò)誤說(shuō)明代碼有錯(cuò)誤,而失敗表示該測(cè)試方法測(cè)試失?。?左下紅框中則表示出了各個(gè)測(cè)試方法的運(yùn)行狀態(tài),可以看到成功、錯(cuò)誤、失敗、失敗各自的圖標(biāo)是不一樣的,還可以看

42、到運(yùn)行時(shí)間。 右邊部分則是異常堆棧,可查看異常信息。4、實(shí)例總結(jié)4.1 參數(shù)化測(cè)試有時(shí)一個(gè)測(cè)試方法,不同的參數(shù)值會(huì)產(chǎn)生不同的結(jié)果,那么我們?yōu)榱藴y(cè)試全面,會(huì)把多個(gè)參數(shù)值都寫(xiě)出來(lái)并一一斷言測(cè)試,這樣有時(shí)難免費(fèi)時(shí)費(fèi)力,這是我們便可以采用參數(shù)化測(cè)試來(lái)解決這個(gè)問(wèn)題。參數(shù)化測(cè)試就好比把一個(gè)“輸入值,期望值”的集合傳入給測(cè)試方法,達(dá)到一次性測(cè)試的目的。1. package test;  2.   3. import static org.junit.Assert.*;  4.   5. import

43、 java.util.Arrays;  6.   7. import org.junit.Test;  8. import org.junit.runner.RunWith;  9. import org.junit.runners.Parameterized;  10. import org.junit.runners.Parameterized.Parameters;  11.   12. RunWit

44、h(Parameterized.class)  13. public class FibonacciTest   14.   15.     Parameters(name = "index: fib(0)=1")  16.     public static Iterable<Object> data() 

45、;  17.         return Arrays.asList(new Object   0, 0 ,  1, 1 ,  2, 1 ,  18.               

46、0;  3, 2 ,  4, 3 ,  5, 5 ,  6, 8  );  19.       20.   21.     private int input;  22.     private in

47、t expected;  23.   24.     public FibonacciTest(int input, int expected)   25.         this.input = input;  26.         

48、this.expected = expected;  27.       28.   29.     Test  30.     public void test()   31.         assertEquals(expected, 

49、;Fpute(input);  32.       33.   34.   35. class Fibonacci   36.   37.     public static int compute(int input)   38.       &#

50、160; int result;  39.         switch (input)   40.         case 0:  41.             result = 0;

51、  42.             break;  43.         case 1:  44.         case 2:  45.       &#

52、160;     result = 1;  46.             break;  47.         case 3:  48.           

53、;  result = 2;  49.             break;  50.         case 4:  51.             result&#

54、160;= 3;  52.             break;  53.         case 5:  54.             result = 5; &#

55、160;55.             break;  56.         case 6:  57.             result = 8;  58.   &

56、#160;         break;  59.         default:  60.             result = 0;  61.       &#

57、160;   62.         return result;  63.       64.   Parameters注解參數(shù)name,實(shí)際是測(cè)試方法名稱。由于一個(gè)test()方法就完成了所有測(cè)試,那假如某一組測(cè)試數(shù)據(jù)有問(wèn)題,那在Junit的結(jié)果頁(yè)面里該如何呈現(xiàn)?因此采用name實(shí)際上就是區(qū)分每個(gè)測(cè)試數(shù)據(jù)的測(cè)試方法名。如下圖:4.2 打包測(cè)試同樣,如果一個(gè)項(xiàng)目中有很多個(gè)測(cè)試

58、用例,如果一個(gè)個(gè)測(cè)試也很麻煩,因此打包測(cè)試就是一次性測(cè)試完成包中含有的所有測(cè)試用例。1. package test;  2.   3. import org.junit.runner.RunWith;  4. import org.junit.runners.Suite;  5.   6. RunWith(Suite.class)  7. Suite.SuiteClasses( AssertTests.class, Fibon

59、acciTest.class, JDemoTest.class )  8. public class AllCaseTest   9.   10.   這個(gè)功能也需要使用一個(gè)特殊的Runner ,需要向RunWith注解傳遞一個(gè)參數(shù)Suite.class 。同時(shí),我們還需要另外一個(gè)注解Suite.SuiteClasses,來(lái)表明這個(gè)類是一個(gè)打包測(cè)試類。并將需要打包的類作為參數(shù)傳遞給該注解就可以了。至于AllCaseTest隨便起一個(gè)類名,內(nèi)容為空既可。運(yùn)行AllCa

60、seTest類即可完成打包測(cè)試4.3 異常測(cè)試異常測(cè)試與普通斷言測(cè)試不同,共有三種方法,其中最為靈活的是第三種,可以與斷言結(jié)合使用 第一種:1. Test(expected= IndexOutOfBoundsException.class)   2. public void empty()    3.      new ArrayList<Object>().get(0);   4.  

61、;  第二種:1. Test  2. public void testExceptionMessage()   3.     try   4.         new ArrayList<Object>().get(0);  5.         

62、fail("Expected an IndexOutOfBoundsException to be thrown");  6.      catch (IndexOutOfBoundsException anIndexOutOfBoundsException)   7.         assertThat(anIndexOut

63、OfBoundsException.getMessage(), is("Index: 0, Size: 0");  8.       9.    第三種:1. Rule  2. public ExpectedException thrown = ExpectedException.none();  3.   4. Test &#

64、160;5. public void shouldTestExceptionMessage() throws IndexOutOfBoundsException   6.     List<Object> list = new ArrayList<Object>();  7.   8.     thrown.expect(IndexOutOfB

65、oundsException.class);  9.     thrown.expectMessage("Index: 0, Size: 0");  10.     list.get(0);  11.     Assert.assertEquals(1, list.get(0);  12.   在上述幾種方法中,無(wú)論是

66、expected還是expect都表示期望拋出的異常,假如某一方法,當(dāng)參數(shù)為某一值時(shí)會(huì)拋出異常,那么使用第一種方法就必須為該參數(shù)單獨(dú)寫(xiě)一個(gè)測(cè)試方法來(lái)測(cè)試異常,而無(wú)法與其他參數(shù)值一同寫(xiě)在一個(gè)測(cè)試方法里,所以顯得累贅。第二種方法雖然解決這個(gè)問(wèn)題,但是寫(xiě)法不僅繁瑣也不利于理解。而第三種犯法,不僅能動(dòng)態(tài)更改期望拋出的異常,與斷言語(yǔ)句結(jié)合的也非常好,因此推薦使用該方法來(lái)測(cè)試異常。4.4 限時(shí)測(cè)試有時(shí)為了防止出現(xiàn)死循環(huán)或者方法執(zhí)行過(guò)長(zhǎng)(或檢查方法效率),而需要使用到限時(shí)測(cè)試。顧名思義,就是超出設(shè)定時(shí)間即視為測(cè)試失敗。共有兩種寫(xiě)法。 第一種:1. Test(timeout=1000)  

67、2. public void testWithTimeout()   3.   .  4.    第二種:1. public class HasGlobalTimeout   2.     public static String log;  3.   4.     Rule &

68、#160;5.     public Timeout globalTimeout = new Timeout(10000); / 10 seconds max per method tested  6.   7.     Test  8.     public void test

69、InfiniteLoop1()   9.         log += "ran1"  10.         for (;)   11.           12.    

70、60;  13.   14.     Test  15.     public void testInfiniteLoop2()   16.         log += "ran2"  17.       &

71、#160; for (;)   18.           19.       20.   其中,第二種方法與異常測(cè)試的第三種方法的寫(xiě)法類似。也是推薦的寫(xiě)法。5、Spring測(cè)試套件5.1 會(huì)用Spring測(cè)試套件的好處在開(kāi)發(fā)基于Spring的應(yīng)用時(shí),如果你還直接使用Junit進(jìn)行單元測(cè)試,那你就錯(cuò)過(guò)了Spring為我們所提供的饕餮大餐了。使用Junit直接進(jìn)行單元測(cè)

72、試有以下四大不足: 導(dǎo)致多次Spring容器初始化問(wèn)題 根據(jù)JUnit測(cè)試方法的調(diào)用流程,每執(zhí)行一個(gè)測(cè)試方法都會(huì)創(chuàng)建一個(gè)測(cè)試用例的實(shí)例并調(diào)用setUp()方法。由于一般情況下,我們?cè)趕etUp()方法中初始化Spring容器,這意味著如果測(cè)試用例有多少個(gè)測(cè)試方法,Spring容器就會(huì)被重復(fù)初始化多次。雖然初始化Spring容器的速度并不會(huì)太慢,但由于可能會(huì)在Spring容器初始化時(shí)執(zhí)行加載Hibernate映射文件等耗時(shí)的操作,如果每執(zhí)行一個(gè)測(cè)試方法都必須重復(fù)初始化Spring容器,則對(duì)測(cè)試性能的影響是不容忽視的; 使用Spring測(cè)試套件,Spring容器只會(huì)初始化一次 需要使用硬編碼方式手

73、工獲取Bean 在測(cè)試用例類中我們需要通過(guò)ctx.getBean()方法從Spirng容器中獲取需要測(cè)試的目標(biāo)Bean,并且還要進(jìn)行強(qiáng)制類型轉(zhuǎn)換的造型操作。這種乏味的操作迷漫在測(cè)試用例的代碼中,讓人覺(jué)得煩瑣不堪; 使用Spring測(cè)試套件,測(cè)試用例類中的屬性會(huì)被自動(dòng)填充Spring容器的對(duì)應(yīng)Bean,無(wú)須在手工設(shè)置Bean! 數(shù)據(jù)庫(kù)現(xiàn)場(chǎng)容易遭受破壞 測(cè)試方法對(duì)數(shù)據(jù)庫(kù)的更改操作會(huì)持久化到數(shù)據(jù)庫(kù)中。雖然是針對(duì)開(kāi)發(fā)數(shù)據(jù)庫(kù)進(jìn)行操作,但如果數(shù)據(jù)操作的影響是持久的,可能會(huì)影響到后面的測(cè)試行為。舉個(gè)例子,用戶在測(cè)試方法中插入一條ID為1的User記錄,第一次運(yùn)行不會(huì)有問(wèn)題,第二次運(yùn)行時(shí),就會(huì)因?yàn)橹麈I沖突而導(dǎo)

74、致測(cè)試用例失敗。所以應(yīng)該既能夠完成功能邏輯檢查,又能夠在測(cè)試完成后恢復(fù)現(xiàn)場(chǎng),不會(huì)留下“后遺癥”; 使用Spring測(cè)試套件,Spring會(huì)在你驗(yàn)證后,自動(dòng)回滾對(duì)數(shù)據(jù)庫(kù)的操作,保證數(shù)據(jù)庫(kù)的現(xiàn)場(chǎng)不被破壞,因此重復(fù)測(cè)試不會(huì)發(fā)生問(wèn)題! 不方便對(duì)數(shù)據(jù)操作正確性進(jìn)行檢查 假如我們向登錄日志表插入了一條成功登錄日志,可是我們卻沒(méi)有對(duì)t_login_log表中是否確實(shí)添加了一條記錄進(jìn)行檢查。一般情況下,我們可能是打開(kāi)數(shù)據(jù)庫(kù),肉眼觀察是否插入了相應(yīng)的記錄,但這嚴(yán)重違背了自動(dòng)測(cè)試的原則。試想在測(cè)試包括成千上萬(wàn)個(gè)數(shù)據(jù)操作行為的程序時(shí),如何用肉眼進(jìn)行檢查? 只要你繼承Spring的測(cè)試套件的用例類,你就可以通過(guò)jdb

75、cTemplate(或Dao等)在同一事務(wù)中訪問(wèn)數(shù)據(jù)庫(kù),查詢數(shù)據(jù)的變化,驗(yàn)證操作的正確性!Spring提供了一套擴(kuò)展于Junit測(cè)試用例的測(cè)試套件,使用這套測(cè)試套件完全解決了以上四個(gè)問(wèn)題,讓我們測(cè)試Spring的應(yīng)用更加方便。這個(gè)測(cè)試套件主要由org.springframework.test包下的若干類組成,使用簡(jiǎn)單快捷,方便上手。5.2 使用方法 基本用法1. package com.test;  2.   3. import javax.annotation.Resource;  4.   

76、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(SpringJ

77、Unit4ClassRunner.class)  11. ContextConfiguration(locations =  "classpath:config/applicationContext-*.xml", "classpath:services/ext/service-*.xml" )  12. public class UserServiceTest   13.   14.  

78、;   Resource  15.     private IUserService userService;  16.   17.     Test  18.     public void testAddOpinion1()   19.      

79、;   userService.downloadCount(1);  20.         System.out.println(1);  21.       22.   23.     Test  24.     public void test

80、AddOpinion2()   25.         userService.downloadCount(2);  26.         System.out.println(2);  27.       28.   RunWith(SpringJUnit4ClassRunner.cl

81、ass) 用于配置spring中測(cè)試的環(huán)境ContextConfiguration(locations = "classpath:config/applicationContext-*.xml", "classpath:services/ext/service-*.xml" )用于指定配置文件所在的位置Resource注入Spring容器Bean對(duì)象,注意與Autowired區(qū)別 事務(wù)用法1. package com.test;  2.   3. import javax.annotation.

82、Resource;  4.   5. import org.junit.Test;  6. import org.junit.runner.RunWith;  7. import org.springframework.test.annotation.Rollback;  8. import org.springframework.test.context.ContextConfiguration;  9. import or

83、g.springframework.test.context.junit4.SpringJUnit4ClassRunner;  10. import org.springframework.test.context.transaction.TransactionConfiguration;  11. import org.springframework.transaction.annotation.Transactional;  12.   13. RunWith(SpringJUnit4Cla

84、ssRunner.class)  14. ContextConfiguration(locations =  "classpath:config/applicationContext-*.xml", "classpath:services/ext/service-*.xml" )  15. Transactional  16. TransactionConfiguration(transactionManager = &quo

85、t;transactionManager")  17. /TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)  18. public class UserServiceTest   19.   20.     Resource 

86、 21.     private IUserService userService;  22.   23.     Test  24. /  Transactional  25.     public void testAddOpinion1()   26.    &#

87、160;    userService.downloadCount(1);  27.         System.out.println(1);  28.       29.   30.     Test  31.     Rollback(false)&#

88、160; 32.     public void testAddOpinion2()   33.         userService.downloadCount(2);  34.         System.out.println(2);  35.    

89、0;  36.   TransactionConfiguration(transactionManager="transactionManager")讀取Spring配置文件中名為transactionManager的事務(wù)配置,defaultRollback為事務(wù)回滾默認(rèn)設(shè)置。該注解是可選的,可使用Transactional與Rollback配合完成事務(wù)管理。當(dāng)然也可以使用Transactional與TransactionConfiguration配合。Transactional開(kāi)啟事務(wù)。可放到類或方法上,類上作用于所有方法。Roll

90、back事務(wù)回滾配置。只能放到方法上。 繼承AbstractTransactionalJUnit4SpringContextTests1. package com.test;  2.   3. import javax.annotation.Resource;  4.   5. import org.junit.Test;  6. import org.springframework.test.context.ContextConfiguration;&#

91、160; 7. import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;  8. import org.springframework.test.context.transaction.TransactionConfiguration;  9.   10. ContextConfiguration(locations =  "classp

92、ath:config/applicationContext-*.xml", "classpath:services/ext/service-*.xml" )  11. TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)  12. public class UserServiceTest

93、 extends AbstractTransactionalJUnit4SpringContextTests   13.   14.     Resource  15.     private IUserService userService;  16.   17.     Test  18.  &

94、#160;  public void testAddOpinion1()   19.         userService.downloadCount(1);  20.         System.out.println(1);  21.       22. 

95、60; 23.     Test  24.     public void testAddOpinion2()   25.         userService.downloadCount(2);  26.         System.out.printl

96、n(2);  27.       28.   AbstractTransactionalJUnit4SpringContextTests:這個(gè)類為我們解決了在web.xml中配置OpenSessionInview所解決的session生命周期延長(zhǎng)的問(wèn)題,所以要繼承這個(gè)類。該類已經(jīng)在類級(jí)別預(yù)先配置了好了事物支持,因此不必再配置Transactional和RunWith 繼承1. package com.test;  2.   3. import&#

97、160;org.springframework.test.context.ContextConfiguration;  4. import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;  5. import org.springframework.test.context.transaction.TransactionConfiguration;  6.   7. Co

98、ntextConfiguration(locations =  "classpath:config/applicationContext-*.xml", "classpath:services/ext/service-*.xml" )  8. TransactionConfiguration(transactionManager = "transactionManager")  9. public class

99、0;BaseTestCase extends AbstractTransactionalJUnit4SpringContextTests   10.   11.   1. package com.test;  2.   3. import javax.annotation.Resource;  4.   5. import org.junit.Test;  6. import&

100、#160;org.springframework.test.annotation.Rollback;  7.   8. public class UserServiceTest extends BaseTestCase   9.   10.     Resource  11.     private IUserService userService

101、;  12.   13.     Test  14.     public void testAddOpinion1()   15.         userService.downloadCount(1);  16.        

102、60;System.out.println(1);  17.       18.   19.     Test  20.     Rollback(false)  21.     public void testAddOpinion2()   22.    &

103、#160;    userService.downloadCount(2);  23.         System.out.println(2);  24.       25.    綜合1. RunWith(SpringJUnit4ClassRunner.class)  2. ContextConfiguration 

104、60;3. TransactionConfiguration  4. Transactional  5. public class PersonDaoTransactionUnitTest extends AbstractTransactionalJUnit4SpringContextTests   6.   7.     final Logger logger = LoggerFact

105、ory.getLogger(PersonDaoTransactionUnitTest.class);  8.   9.     protected static int SIZE = 2;  10.     protected static Integer ID = new Integer(1);  11.  

106、0;  protected static String FIRST_NAME = "Joe"  12.     protected static String LAST_NAME = "Smith"  13.     protected static String CHANGED_LAST_NAME =

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論