教你快速写出多线程Junit单元测试用例 - GroboUtils

 本文出自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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值