写过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,解压后 使用 GroboUtils-5\lib\core\GroboTestingJUnit-1.2.1-core.jar 这个即可

GroboUtils是一个工具集合,里面包含各种测试工具,这里使用的是该工具集中的jUnit扩展.

依赖好Jar包后就可以编写多线程测试用例了。上手很简单:

package com.junittest.threadtest;  

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set; import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner;
import net.sourceforge.groboutils.junit.v1.TestRunnable; import org.junit.Test; public class MutiThreadTest { static String[] path = new String[] { "" };
static Map<String, String> countMap = new Hashtable<String, String>();
static Map<String, String> countMap2 = new Hashtable<String, String>();
static Set<String> countSet = new HashSet<String>();
static List<String> list = new ArrayList<String>(); @Test
public void testThreadJunit() throws Throwable {
//Runner数组,想当于并发多少个。
TestRunnable[] trs = new TestRunnable [10];
for(int i=0;i<10;i++){
trs[i]=new ThreadA();
} // 用于执行多线程测试用例的Runner,将前面定义的单个Runner组成的数组传入
MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs);
// 开发并发执行数组里定义的内容
mttr.runTestRunnables();
} private class ThreadA extends TestRunnable {
@Override
public void runTest() throws Throwable {
// 测试内容
myCommMethod2();
}
} public void myCommMethod2() throws Exception {
System.out.println("===" + Thread.currentThread().getId() + "begin to execute myCommMethod2");
for (int i = 0; i <10; i++) {
int a = i*5;
System.out.println(a);
}
System.out.println("===" + Thread.currentThread().getId() + "end to execute myCommMethod2");
}
}

运行时需依赖log4j的jar文件,GroboUtils的jar包。

主要关注3个类:TestRunnable,TestMonitorRunnable,MultiThreadedTestRunner,全部来自包:net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner.

(1) TestRunnable 抽象类,表示一个测试线程,实例需要实现该类的runTest()方法,在该方法中写自己用的测试代码.

该类继承了jUnit的junit.framework.Assert类,所以可以在TestRunnable中使用各种Assert方法

(可惜因为GroboUtils使用的jUnit版本较老,且久未更新,新版本的jUnit中已经不推荐使用这个类的方法了).

该类实现了Runnable,在run方法中调用抽象方法runTest().

(2)   MultiThreadedTestRunner

这个类相当与一个ExecuteService,可以用来执行 TestRunnable,构造函数需要传入TestRunnable数组,

表示需要测试的线程.

调用MultiThreadedTestRunner.runTestRunnables() 方法启动测试线程,开始执行测试.

这个方法默认让测试线程TestRunnable的run方法最多运行1天,也可以调用

MultiThreadedTestRunner.runTestRunnables(long maxTime) 这个方法,然测试线程TestRunnable

最多执行 maxTime 毫秒.如果超过maxTime毫秒之后,TestRunnable还没有执行完毕,则TestRunnable

会被中断,并且MultiThreadedTestRunner 会抛出异常,

导致测试失败fail("Threads did not finish within " + maxTime + " milliseconds.").

每个TestRunnable中runTest需要能够控制自己在什么时间自己结束自己,精确控制测试时间,不要利用

上面的maxTime.

(3) TestMonitorRunnable

表示监控线程,可以让每一个TestRunnable对应一个TestMonitorRunnable,在TestMonitorRunnable中监控

TestRunnable.TestMonitorRunnable是TestRunnable的子类,提供了一些方便使用的方法.

Junit使用GroboUtils进行多线程测试的更多相关文章

  1. junit基础学习之-多线程测试(6)

    步骤: 1.定义单个TestRunner 2.重载单个TestRunner的runTest() 3.定义TestRunner数组,并添加多个TestRunner 4.MultiThreadedTest ...

  2. JUNIT4 GroboUtils多线程测试

    阅读更多 利用JUNIT4,GroboUtils进行多线程测试 多线程编程和测试一直是比较难搞的事情,特别是多线程测试.只用充分的测试,才可以发现多线程编码的潜在BUG.下面就介绍一下我自己在测试多线 ...

  3. Junit借助Groboutils Core进行并发测试

    本文参考:http://www.voidcn.com/article/p-ybnvuffh-ke.html:转载请注明出处 junit是无法进行并发测试,但是又有需要并发测试的场景怎么办呢?此时可以借 ...

  4. 关于JUnit4无法支持多线程测试的解决方法

    转自:https://segmentfault.com/a/1190000003762719 其实junit是将test作为参数传递给了TestRunner的main函数.并通过main函数进行执行. ...

  5. 「译」JUnit 5 系列:条件测试

    原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...

  6. JUnit使用参数测试和一组测试

    JUnit该参数测试和一组测试使用简单 参数测试 作为替代阵列int a0,a1,a2喜欢,当测试加法assertEquals(3.0, h.add(1, 2), 0.1);相当于声明一个变量,要測试 ...

  7. testng入门教程12 TestNG执行多线程测试

    testng入门教程 TestNG执行多线程测试 testng入门教程 TestNG执行多线程测试 并行(多线程)技术在软件术语里被定义为软件.操作系统或者程序可以并行地执行另外一段程序中多个部分或者 ...

  8. JUnit 3.8 通过反射测试私有方法

    测试私有(private)的方法有两种: 1)把目标类的私有方法(修饰符:private)修改为(public),不推荐,因为修改了源程序不佳 2)通过反射 (推荐) 代码演示: 目标程序 Priva ...

  9. TestNG多线程测试-注解方式实现

    用@Test(invocationCount = x,threadPoolSize = y)声明,invocationCount表示执行次数,threadPoolSize表示线程池大小. packag ...

随机推荐

  1. android开发布局文件imageview 图片等比例缩放:

    ImageView的属性scaleType,如果等比缩放的话,就使用CenterInside,如果想固定大小的话,就CenterCrop <?xml version="1.0" ...

  2. java微信开发(wechat4j)——wechat4j配置文件解读

    wechat4j的配置文件是wechat4j.properties.需要放置在项目src目录下.在wechat4j.jar中的META-INF下有一个wechat4j.properties.sampl ...

  3. 【翻译】配置RSVP-signaled LSP

    源地址: https://www.juniper.net/techpubs/software/junos-security/junos-security10.2/junos-security-swco ...

  4. JavaScript焦点轮播图

    在慕课学习了JavaScript焦点轮播图特效,在此做一个整理. 首先是html结构,我用的是本地同文件夹下的三张图片,多出来的第一张(pic3副本)和最后一张图片(pic1副本)是为了实现无缝切换效 ...

  5. ArcGIS中定义图框样式

    ArcGIS系统中的样式可能不能满足实际生产需要,为了实现快速制图,可自定义一些样式,以便重复利用. 安装字符 因为样式中定义了自定义的符号,这些符号都打包到字体中,所以在使用样式之前,必须安装字体文 ...

  6. 转发离线安装 Android Studio 更新

    1.在线更新 随着 Android Studio 的越来越完善与流行,无论从功能性,还是性能上,它正在成为广大 Android 开发者的首选.但是因为总所周知墙的原因,我们在 Android Stud ...

  7. 【原】画流程图工具visio使用技巧汇总

    最近写论文需要画不少流程图,有两种选择,一是word, 二是visio.原先一直用word画,效果也还可以,但是每个部件画完后为了便于适应排版变动,需要将需要的模块按下ctrl逐个点击选中后进行组合. ...

  8. iOS 学习 - 5.UILabel设置行距

    NSMutableAttributedString *arrString =[[NSMutableAttributedString alloc]initWithString:@"asdass ...

  9. Effective Java 18 Prefer interfaces to abstract classes

    Feature Interface Abstract class Defining a type that permits multiple implementations Y Y Permitted ...

  10. MS SqlServer学习笔记(索引)

    1.索引分类 MS SqlServer提供了两种索引:聚集索引和非聚集索引: 聚集索引是将数据按照索引的顺序存放 非聚集索引是将索引和数据分离存放,通过指针将二者联系到一起. 因为两种索引对比: 使用 ...