日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

多线程的单元测试工具 - GroboUtils

發布時間:2024/7/19 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 多线程的单元测试工具 - GroboUtils 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

  • /**?
  • ?????*?Run?the?tests?contained?in?the?classes?named?in?the?<code>args</code>.?
  • ?????*?If?all?tests?run?successfully,?exit?with?a?status?of?0.?Otherwise?exit?with?a?status?of?1.?
  • ?????*?Write?feedback?while?tests?are?running?and?write?
  • ?????*?stack?traces?for?all?failed?tests?after?the?tests?all?complete.?
  • ?????*?@param?args?names?of?classes?in?which?to?find?tests?to?run?
  • ?????*/?
  • ????public?static?void?main(String...?args)?{?
  • ????????runMainAndExit(new?RealSystem(),?args);?
  • ????}?
  • ?
  • ????/**?
  • ?????*?Do?not?use.?Testing?purposes?only.?
  • ?????*?@param?system??
  • ?????*/?
  • ????public?static?void?runMainAndExit(JUnitSystem?system,?String...?args)?{?
  • ????????Result?result=?new?JUnitCore().runMain(system,?args);?
  • ????????system.exit(result.wasSuccessful()???0?:?1);?
  • ????}?
  • ?

    RealSystem.java:

  • public?void?exit(int?code)?{?
  • ?
  • ????????System.exit(code);?
  • ?
  • ????}?
  • ?

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

    ?

    ?

    注:需要第三方庫支持:
    RepositoryOpensymphony Releases
    Repository urlhttps://oss.sonatype.org/content/repositories/opensymphony-releases
    依賴好Jar包后就可以編寫多線程測試用例了。上手很簡單:
  • /**?
  • ?????*?多線程測試用例?
  • ?????*??
  • ?????*?@author?lihzh(One?Coder)?
  • ?????*?@date?2012-6-12?下午9:18:11?
  • ?????*?@blog?http://www.coderli.com?
  • ?????*/?
  • ????@Test?
  • ????public?void?MultiRequestsTest()?{?
  • ????????????????//?構造一個Runner?
  • ????????TestRunnable?runner?=?new?TestRunnable()?{?
  • ????????????@Override?
  • ????????????public?void?runTest()?throws?Throwable?{?
  • ????????????????//?測試內容?
  • ????????????}?
  • ????????};?
  • ????????int?runnerCount?=?100;?
  • ????????????????//Rnner數組,想當于并發多少個。?
  • ????????TestRunnable[]?trs?=?new?TestRunnable[runnerCount];?
  • ????????for?(int?i?=?0;?i?<?runnerCount;?i++)?{?
  • ????????????trs[i]?=?runner;?
  • ????????}?
  • ????????????????//?用于執行多線程測試用例的Runner,將前面定義的單個Runner組成的數組傳入?
  • ????????MultiThreadedTestRunner?mttr?=?new?MultiThreadedTestRunner(trs);?
  • ????????try?{?
  • ????????????????????????//?開發并發執行數組里定義的內容?
  • ????????????mttr.runTestRunnables();?
  • ????????}?catch?(Throwable?e)?{?
  • ????????????e.printStackTrace();?
  • ????????}?
  • ????}?
  • ?

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

    本文出自One Coder博客,出處:?http://www.coderli.com/archives/multi-thread-junit-grobountils/

    轉載于:https://www.cnblogs.com/xujanus/p/5530444.html

    總結

    以上是生活随笔為你收集整理的多线程的单元测试工具 - GroboUtils的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。