Junit应用GroboUtils进行多线程测试

Junit使用GroboUtils进行多线程测试
用JUNIT4,GroboUtils进行多线程测试

多线程编程和测试一直是比较难搞的事情,特别是多线程测试。只用充分的测试,才可以发现多线程编码的潜在BUG。下面就介绍一下我自己在测试多线程并发程序时用的一个比较简单好用的测试工具类库。即JUNIT4和GroboUtils。
废话不多说,把代码贴出来,大家一看就明白了。

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 {
		
		TestRunnable[] trs = new TestRunnable [10];
		for(int i=0;i<10;i++){
			trs[i]=new ThreadA();
		}

		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");
	}
}



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

参考文章
[/url]http://www.cnblogs.com/shwen99/archive/2010/03/22/1691280.html[url]
该文章也介绍了ConTest测试工具,比GroboUtils更强大的多线程测试工具,但好像不是免费的。有兴趣的可以深入了解一下。