教你快速写出多线程Junit单元测试用例 - GroboUtils
2016-05-06 11:18
489 查看
摘自: http://mushiqianmeng.blog.51cto.com/3970029/897786/
本文出自One Coder博客,转载请务必注明出处: http://www.coderli.com/archives/multi-thread-junit-grobountils/
写过Junit单元测试的同学应该会有感觉,Junit本身是不支持普通的多线程测试的,这是因为Junit的底层实现上,是用System.exit退出用例执行的。JVM都终止了,在测试线程启动的其他线程自然也无法执行。JunitCore代码如下:
/**
* Run the tests contained in the classes named in the
* 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);
}
RealSystem.java:
public void exit(int code) {
所以要想编写多线程Junit测试用例,就必须让主线程等待所有子线程执行完成后再退出。想到的办法自然是Thread中的join方法。话又说回来,这样一个简单而又典型的需求,难道会没有第三方的包支持么?通过google,笔者很快就找到了GroboUtils这个Junit多线程测试的开源的第三方的工具包。
GroboUtils官网如下:
http://groboutils.sourceforge.net/
下载页面:
http://groboutils.sourceforge.net/downloads.html
Maven依赖方式:
net.sourceforge.groboutils
groboutils-core
5
注:需要第三方库支持:
Repository Opensymphony Releases
Repository url https://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/
本文出自 “苦逼coder” 博客,转载请与作者联系!
本文出自One Coder博客,转载请务必注明出处: http://www.coderli.com/archives/multi-thread-junit-grobountils/
写过Junit单元测试的同学应该会有感觉,Junit本身是不支持普通的多线程测试的,这是因为Junit的底层实现上,是用System.exit退出用例执行的。JVM都终止了,在测试线程启动的其他线程自然也无法执行。JunitCore代码如下:
/**
* Run the tests contained in the classes named in the
args.
* 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依赖方式:
net.sourceforge.groboutils
groboutils-core
5
注:需要第三方库支持:
Repository Opensymphony Releases
Repository url https://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/
本文出自 “苦逼coder” 博客,转载请与作者联系!
相关文章推荐
- centos jenkins
- ArcGIS相关帮助网址大全
- UITextField左侧的表达图片(..我也不知道叫什么)
- Python 迭代器工具包【推荐】
- 重读java编程思想之访问权限控制二
- 你应该知道的HTTP基础知识
- 2016年4月27日作业(论文)
- PHP递归遍历多维数组实现无限分类的方法
- 镭射激光HSL-4000III二次开发
- bootstrap制作导航条
- 快速排序里的学问:再看看称球问题
- Java 如何有效地避免OOM:善于利用软引用和弱引用
- jquery form 表单ajax提交使用
- Java中的多态的用途
- H.264整数DCT公式推导及蝶形算法分析
- [团队项目] Scrum 项目 2.0 产品BACKLOG
- 解决Linux中文显示乱码的问题
- c#省市联动
- HTML5 input元素新的特性
- 使用SharedPreferences存储android用户信息