写过Junit单元测试的同窗应该会有感受,Junit自己是不支持普通的多线程测试的,这是由于Junit的底层实现上,是用System.exit退出用例执行的。JVM都终止了,在测试线程启动的其余线程天然也没法执行。JunitCore代码以下:html
/** * 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: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扩展.maven
依赖好Jar包后就能够编写多线程测试用例了。上手很简单:ide
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的子类,提供了一些方便使用的方法.
注:maven工程须要本身下载groboutils-core-5.jar ,而后在私服上上传安装下,将dependency添加到本身的pom.xml中便可。