Junit使用GroboUtils进行多线程测试

写过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); 
    } 
</span><span style="color: #008000;">/**</span><span style="color: #008000;"> 
 * Do not use. Testing purposes only. 
 * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> system  
 </span><span style="color: #008000;">*/</span> 
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> runMainAndExit(JUnitSystem system, String... args) { 
    Result result</span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> JUnitCore().runMain(system, args); 
    system.exit(result.wasSuccessful() </span>? 0 : 1<span style="color: #000000;">); 
} </span></pre>
复制代码

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-5libcoreGroboTestingJUnit-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 {

</span><span style="color: #0000ff;">static</span> String[] path = <span style="color: #0000ff;">new</span> String[] { ""<span style="color: #000000;"> };  
</span><span style="color: #0000ff;">static</span> Map&lt;String, String&gt; countMap = <span style="color: #0000ff;">new</span> Hashtable&lt;String, String&gt;<span style="color: #000000;">();  
</span><span style="color: #0000ff;">static</span> Map&lt;String, String&gt; countMap2 = <span style="color: #0000ff;">new</span> Hashtable&lt;String, String&gt;<span style="color: #000000;">();  
</span><span style="color: #0000ff;">static</span> Set&lt;String&gt; countSet = <span style="color: #0000ff;">new</span> HashSet&lt;String&gt;<span style="color: #000000;">();  
</span><span style="color: #0000ff;">static</span> List&lt;String&gt; list = <span style="color: #0000ff;">new</span> ArrayList&lt;String&gt;<span style="color: #000000;">();  

@Test  
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> testThreadJunit() <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Throwable {   
    </span><span style="color: #008000;">//</span><span style="color: #008000;">Runner数组,想当于并发多少个。 </span>
    TestRunnable[] trs = <span style="color: #0000ff;">new</span> TestRunnable [10<span style="color: #000000;">];  
    </span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> i=0;i&lt;10;i++<span style="color: #000000;">){  
        trs[i]</span>=<span style="color: #0000ff;">new</span><span style="color: #000000;"> ThreadA();  
    }  

    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 用于执行多线程测试用例的Runner,将前面定义的单个Runner组成的数组传入 </span>
    MultiThreadedTestRunner mttr = <span style="color: #0000ff;">new</span><span style="color: #000000;"> MultiThreadedTestRunner(trs);  
    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 开发并发执行数组里定义的内容 </span>

mttr.runTestRunnables();
}

</span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">class</span> ThreadA <span style="color: #0000ff;">extends</span><span style="color: #000000;"> TestRunnable {  
    @Override  
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> runTest() <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Throwable {  
        </span><span style="color: #008000;">//</span><span style="color: #008000;"> 测试内容</span>

myCommMethod2();
}
}

</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> myCommMethod2() <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception {  
    System.out.println(</span>"===" + Thread.currentThread().getId() + "begin to execute myCommMethod2"<span style="color: #000000;">);  
    </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt;10; i++<span style="color: #000000;">) {  
         </span><span style="color: #0000ff;">int</span> a  = i*5<span style="color: #000000;">;  
         System.out.println(a);  
    }  
    System.out.println(</span>"===" + Thread.currentThread().getId() + "end to execute myCommMethod2"<span style="color: #000000;">);  
}  

}

复制代码

运行时需依赖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的子类,提供了一些方便使用的方法.

原文地址:https://www.cnblogs.com/yangxia-test/p/4022037.html
原文地址:https://www.cnblogs.com/jpfss/p/11582529.html