Junit spring 多線程測試

本文出自One Coder博客,轉載請務必註明出處: http://www.coderli.com/archives/multi-thread-junit-grobountils/

寫過Junit單元測試的同學應該會有感覺,Junit本身是不支持普通的多線程測試的,這是因爲Junit的底層實現上,是用System.exit退出用例執行的。JVM都終止了,在測試線程啓動的其他線程自然也無法執行。JunitCore代碼如下:

  1. /** 
  2.      * Run the tests contained in the classes named in the <code>args</code>. 
  3.      * If all tests run successfully, exit with a status of 0. Otherwise exit with a status of 1. 
  4.      * Write feedback while tests are running and write 
  5.      * stack traces for all failed tests after the tests all complete. 
  6.      * @param args names of classes in which to find tests to run 
  7.      */ 
  8.     public static void main(String... args) { 
  9.         runMainAndExit(new RealSystem(), args); 
  10.     } 
  11.  
  12.     /** 
  13.      * Do not use. Testing purposes only. 
  14.      * @param system  
  15.      */ 
  16.     public static void runMainAndExit(JUnitSystem system, String... args) { 
  17.         Result result= new JUnitCore().runMain(system, args); 
  18.         system.exit(result.wasSuccessful() ? 0 : 1); 
  19.     } 

RealSystem.java:

  1. public void exit(int code) { 
  2.  
  3.         System.exit(code); 
  4.  
  5.     } 

所以要想編寫多線程Junit測試用例,就必須讓主線程等待所有子線程執行完成後再退出。想到的辦法自然是Thread中的join方法。話又說回來,這樣一個簡單而又典型的需求,難道會沒有第三方的包支持麼?通過google,筆者很快就找到了GroboUtils這個Junit多線程測試的開源的第三方的工具包。
 
 GroboUtils官網如下:
 
 
下載頁面:
 
Maven依賴方式:
 
  1. <dependency> 
  2.       <groupId>net.sourceforge.groboutils</groupId> 
  3.       <artifactId>groboutils-core</artifactId> 
  4.       <version>5</version> 
  5.     </dependency> 

注:需要第三方庫支持:
Repository Opensymphony Releases
Repository url https://oss.sonatype.org/content/repositories/opensymphony-releases
依賴好Jar包後就可以編寫多線程測試用例了。上手很簡單:
  1. /** 
  2.      * 多線程測試用例 
  3.      *  
  4.      * @author lihzh(One Coder) 
  5.      * @date 2012-6-12 下午9:18:11 
  6.      * @blog http://www.coderli.com 
  7.      */ 
  8.     @Test 
  9.     public void MultiRequestsTest() { 
  10.                 // 構造一個Runner 
  11.         TestRunnable runner = new TestRunnable() { 
  12.             @Override 
  13.             public void runTest() throws Throwable { 
  14.                 // 測試內容 
  15.             } 
  16.         }; 
  17.         int runnerCount = 100
  18.                 //Rnner數組,想當於併發多少個。 
  19.         TestRunnable[] trs = new TestRunnable[runnerCount]; 
  20.         for (int i = 0; i < runnerCount; i++) { 
  21.             trs[i] = runner; 
  22.         } 
  23.                 // 用於執行多線程測試用例的Runner,將前面定義的單個Runner組成的數組傳入 
  24.         MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs); 
  25.         try { 
  26.                         // 開發併發執行數組裏定義的內容 
  27.             mttr.runTestRunnables(); 
  28.         } catch (Throwable e) { 
  29.             e.printStackTrace(); 
  30.         } 
  31.     } 

執行一下,看看效果。怎麼樣,你的Junit也可以執行多線程測試用例了吧:)。

本文出自One Coder博客,轉載請務必註明出處: http://www.coderli.com/archives/multi-thread-junit-grobountils/

============分割線=====================

Maven使用第三方jar文件的兩種方法

mvn install:install-file -Dfile=groboutils-core-5.jar -DgroupId=net.sourceforge.groboutils -DartifactId=groboutils-core -Dversion=5 -Dpackaging=jar

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章