摘自: 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的更多相关文章

  1. 用聚合数据API(苏州实时公交API)快速写出小程序

    利用聚合数据API快速写出小程序,过程简单. 1.申请小程序账号 2.进入开发 3.调用API.比如“苏州实时公交”小程序,选择的是苏州实时公交API. 苏州实时公交API文档:https://www ...

  2. IDEA最常用快捷键汇总+快速写出Main函数

    IDEA可以说是当下Java程序员日常开发的神器,但是想要发挥这款神器的牛逼威力,必须得熟练使用它的各种快捷键才行.本篇总结下使用IDEA(也就是IntelliJ IDEA )进行日常开发中最常用的快 ...

  3. 【转】SpringTest框架JUnit单元测试用例获取ApplicationContext实例的方法

    转自:http://www.coderli.com/junit-spring-test-applicationcontext JUnit单元测试用例中使用Spring框架,直接方式如下. @RunWi ...

  4. 教你快速写一个EventBus框架

    前言EventBus相信大多数人都用过,其具有方便灵活.解耦性强.体积小.简单易用等优点,虽然现在也有很多优秀的替代方案如RxBus.LiveDataBus等,但不可否认EventBus开创了消息总线 ...

  5. 如何快速写出高质量的 Go 代码?

    前言 团队协作开发中,必然存在着不同的代码风格,并且诸如 http body close,unhandled error 等低级错误不能完全避免.通过使用 ci lint 能够及早的发现并修复问题,提 ...

  6. Zen Coding css,html缩写替换大观 快速写出html,css

    阅读本文,先仔细阅读网站文章. Zen Coding 快速编写HTML/CSS代码的实现 复制代码 代码如下:E 元素名称(div, p); E#id 使用id的元素(div#content, p#i ...

  7. 一个能快速写出实体类的Api文档管理工具

    今天各种MVC框架满天飞,大大降低了编码的难度,写实体类就没有办法回避的一件事了,花大把的时间去做一些重复而且繁琐的工作,实在不是一个优秀程序员的作风,所以多次查找和尝试后,找到一个工具类网站——Ap ...

  8. SpringTest框架JUnit单元测试用例获取ApplicationContext实例的方法

    步骤 1.继承AbstractJUnit4SpringContextTests 2.引入ApplicationContext 示例代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  9. 熟悉Junit单元测试方法

    定义: JUnit是一个Java语言的单元测试框架.它由Kent Beck和Erich Gamma建立,逐渐成为源于Kent Beck的sUnit的xUnit家族中最为成功的一个. JUnit有它自己 ...

随机推荐

  1. POJ 2393 贪心 简单题

    有一家生产酸奶的公司,连续n周,每周需要出货numi的单位,已经知道每一周生产单位酸奶的价格ci,并且,酸奶可以提前生产,但是存储费用是一周一单位s费用,问最少的花费. 对于要出货的酸奶,要不这一周生 ...

  2. Linux中变量$#,$@,$0,$1,$2,$*,$$,$?的含义(转载)

    From:http://dadekey.blog.51cto.com/107327/119938 我们先写一个简单的脚本,执行以后再解释各个变量的意义   # touch variable # vi ...

  3. 关于lib,dll,.a,.so,静态库和动态库的解释说明

    [转]关于lib,dll,.a,.so,静态库和动态库的解释说明 目录 1 什么叫程序库 2 什么是lib,什么是dll,什么是.a,什么是so,什么是静态库,什么是动态库 3 补充说明 4 作者 什 ...

  4. [Flex] ButtonBar系列——简单布局

    <?xml version="1.0" encoding="utf-8"?> <!--通过layout属性,设置ButtonBar布局--&g ...

  5. python 之 模拟GET/POST提交

    以 POST/GET 方式向 http://127.0.0.1:8000/test/index 提交数据. # coding:utf-8 import httplib import urllib cl ...

  6. nyoj 92 图像有用区域

    点击打开链接 图像有用区域 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 "ACKing"同学以前做一个图像处理的项目时,遇到了一个问题,他需要摘取 ...

  7. dubbo服务框架学习

    ====================================================================================== 1.提供注册服务.消费者可 ...

  8. convert NameValueCollection/Dictionary<string, object> to JSON string

    public static class WebExtension { public static T Decode<T>(this RequestBase res) { Type type ...

  9. Qt下QWizard改变next,back等默认按钮的大小及背景

    默认的按钮又小又丑,想改大点漂亮点. 百度没百出来,最后用google 在这里: http://stackoverflow.com/questions/16425575/change-qwizard- ...

  10. 中南大学第一届长沙地区程序设计邀请赛 To Add Which?

    1350: To Add Which? Time Limit: 1 Sec  Memory Limit: 128 MB Description There is an integer sequence ...