教你快速写出多线程Junit单元测试用例 - GroboUtils
摘自: 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 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 本文出自 “苦逼coder” 博客,转载请与作者联系!
教你快速写出多线程Junit单元测试用例 - GroboUtils的更多相关文章
- 用聚合数据API(苏州实时公交API)快速写出小程序
利用聚合数据API快速写出小程序,过程简单. 1.申请小程序账号 2.进入开发 3.调用API.比如“苏州实时公交”小程序,选择的是苏州实时公交API. 苏州实时公交API文档:https://www ...
- IDEA最常用快捷键汇总+快速写出Main函数
IDEA可以说是当下Java程序员日常开发的神器,但是想要发挥这款神器的牛逼威力,必须得熟练使用它的各种快捷键才行.本篇总结下使用IDEA(也就是IntelliJ IDEA )进行日常开发中最常用的快 ...
- 【转】SpringTest框架JUnit单元测试用例获取ApplicationContext实例的方法
转自:http://www.coderli.com/junit-spring-test-applicationcontext JUnit单元测试用例中使用Spring框架,直接方式如下. @RunWi ...
- 教你快速写一个EventBus框架
前言EventBus相信大多数人都用过,其具有方便灵活.解耦性强.体积小.简单易用等优点,虽然现在也有很多优秀的替代方案如RxBus.LiveDataBus等,但不可否认EventBus开创了消息总线 ...
- 如何快速写出高质量的 Go 代码?
前言 团队协作开发中,必然存在着不同的代码风格,并且诸如 http body close,unhandled error 等低级错误不能完全避免.通过使用 ci lint 能够及早的发现并修复问题,提 ...
- Zen Coding css,html缩写替换大观 快速写出html,css
阅读本文,先仔细阅读网站文章. Zen Coding 快速编写HTML/CSS代码的实现 复制代码 代码如下:E 元素名称(div, p); E#id 使用id的元素(div#content, p#i ...
- 一个能快速写出实体类的Api文档管理工具
今天各种MVC框架满天飞,大大降低了编码的难度,写实体类就没有办法回避的一件事了,花大把的时间去做一些重复而且繁琐的工作,实在不是一个优秀程序员的作风,所以多次查找和尝试后,找到一个工具类网站——Ap ...
- SpringTest框架JUnit单元测试用例获取ApplicationContext实例的方法
步骤 1.继承AbstractJUnit4SpringContextTests 2.引入ApplicationContext 示例代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...
- 熟悉Junit单元测试方法
定义: JUnit是一个Java语言的单元测试框架.它由Kent Beck和Erich Gamma建立,逐渐成为源于Kent Beck的sUnit的xUnit家族中最为成功的一个. JUnit有它自己 ...
随机推荐
- java中使用正则表达式
1.用正则表达式分割字符串 Pattern SPLIT2 = Pattern.compile("[,]"); String[] tmpStrings1 = SPLIT2.split ...
- expdp导出数据库
源地址:http://www.cnblogs.com/luluping/archive/2010/03/16/1687093.html 使用EXPDP和IMPDP时应该注意的事项: EXP和IMP是客 ...
- .NET 中文转缩写拼音
public class CNToSpell { /// 汉字转拼音缩写 /// Code By MuseStudio@hotmail.com /// 2004-11-30 /// 要转换的汉字字符串 ...
- [ActionScript 3.0] AS3动画类Tweener中滤镜的运用
package { import caurina.transitions.Tweener; import caurina.transitions.properties.FilterShortcuts; ...
- [ZOJ 1005] Jugs (dfs倒水问题)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5 题目大意:给你6种操作,3个数a,b,n,代表我有两个杯子,第一个杯 ...
- [CF 474E] Pillars (线段树+dp)
题目链接:http://codeforces.com/contest/474/problem/F 意思是给你两个数n和d,下面给你n座山的高度. 一个人任意选择一座山作为起始点,向右跳,但是只能跳到高 ...
- WPF更新数据源
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windo ...
- 错误记录,找不到sqlite dll
Could not load file or assembly'System.Data.SQLite.dll' or one of its depedencies.找不到指定模块. 在CSDN找到解决 ...
- (转)使用Ping获得局域网机器信息
实际编程中会遇到查找局域网机器的情况,这个时候ping命令对我们帮助比较大,一般来说,都是在命令行中输入“ping XXX -t”,下面的方法是在C#编码中使用Ping. 原文地址:http://zh ...
- android menu 开发
menu 分类: 选项菜单(OptionsMenu) 上下文菜单(ContextMenu) 子菜单(SubMenu) 弹出菜单(Popup) 首先说 选项菜单(OptionsMenu) 一.方法介 ...