JUNIT4 GroboUtils多线程测试

利用JUNIT4,GroboUtils进行多线程测试

多线程编程和测试一直是比较难搞的事情,特别是多线程测试。只用充分的测试,才可以发现多线程编码的潜在BUG。下面就介绍一下我自己在测试多线程并发程序时用的一个比较简单好用的测试工具类库。即JUNIT4和GroboUtils。
废话不多说,把代码贴出来,大家一看就明白了。
Java代码   收藏代码
  1. import java.util.ArrayList;  
  2. import java.util.HashSet;  
  3. import java.util.Hashtable;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6. import java.util.Set;  
  7.   
  8. import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner;  
  9. import net.sourceforge.groboutils.junit.v1.TestRunnable;  
  10.   
  11. import org.junit.After;  
  12. import org.junit.Before;  
  13. import org.junit.Test;  
  14. import org.springframework.context.ApplicationContext;  
  15. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  16.   
  17. public class MutiThreadTest {  
  18.     //此处可以声明一些公共变量  
  19.     static ApplicationContext context = null;  
  20.     static String[] path = new String[] { "" };  
  21.     static Map<String, String> countMap = new Hashtable<String, String>();  
  22.     static Map<String, String> countMap2 = new Hashtable<String, String>();  
  23.     static Set<String> countSet = new HashSet<String>();  
  24.     static List<String> list = new ArrayList<String>();  
  25.   
  26.     @Before  
  27.     public void setUp() throws Exception {  
  28.         context = new ClassPathXmlApplicationContext(path);  
  29.     }  
  30.   
  31.     @After  
  32.     public void tearDown() throws Exception {  
  33.         context = null;  
  34.     }  
  35.             /** 
  36.      * JUNIT会运行这个方法,是主线程 
  37.      */  
  38.     @Test  
  39.     public void testThreadJunit() throws Throwable {  
  40.         //TestRunnable,实例化自定义的7个线程  
  41.         TestRunnable tr1, tr2, tr3, tr4, tr5, tr6, tr7;  
  42.         tr1 = new ThreadA();  
  43.         tr2 = new ThreadB();  
  44.         tr3 = new ThreadC();  
  45.         tr4 = new ThreadD();  
  46.         tr5 = new ThreadE();  
  47.         tr6 = new ThreadF();  
  48.         tr7 = new ThreadG();  
  49.         //必须声明为一个数组,把该数组当参数传递给 MultiThreadedTestRunner  
  50.         TestRunnable[] trs = { tr1, tr2, tr3, tr4, tr5, tr6, tr7 };  
  51.         //不需改动  
  52.         MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs);  
  53.         //执行MTTR和7线程  
  54.         mttr.runTestRunnables();  
  55.     }  
  56.       
  57.     /** 
  58.      * 要运行多线程,首先要实现自定义的线程</br> 
  59.      * 如下我定义了A,B,C,D,E,F,G七个线程</br> 
  60.      * 注意:自定义线程必须要继承TestRunnable</br> 
  61.      * 并且覆盖runTest()方法 
  62.      * 
  63.      */  
  64.     private class ThreadA extends TestRunnable {  
  65.         @Override  
  66.         public void runTest() throws Throwable {  
  67.             //线程要调用的方法或者要执行的操作  
  68.             myCommMethod2();  
  69.         }  
  70.     }  
  71.   
  72.     private class ThreadB extends TestRunnable {  
  73.         @Override  
  74.         public void runTest() throws Throwable {  
  75.             myCommMethod2();  
  76.         }  
  77.     }  
  78.   
  79.     private class ThreadC extends TestRunnable {  
  80.         @Override  
  81.         public void runTest() throws Throwable {  
  82.             myCommMethod2();  
  83.         }  
  84.     }  
  85.   
  86.     private class ThreadD extends TestRunnable {  
  87.         @Override  
  88.         public void runTest() throws Throwable {  
  89.             myCommMethod2();  
  90.         }  
  91.     }  
  92.   
  93.     private class ThreadE extends TestRunnable {  
  94.         @Override  
  95.         public void runTest() throws Throwable {  
  96.             myCommMethod2();  
  97.         }  
  98.     }  
  99.   
  100.     private class ThreadF extends TestRunnable {  
  101.         @Override  
  102.         public void runTest() throws Throwable {  
  103.             myCommMethod2();  
  104.         }  
  105.     }  
  106.   
  107.     private class ThreadG extends TestRunnable {  
  108.         @Override  
  109.         public void runTest() throws Throwable {  
  110.             myCommMethod2();  
  111.         }  
  112.     }  
  113.   
  114.     /** 
  115.      * 线程要调用的方法。在此方法中</br> 
  116.      * 实现你的多线程代码测试。 
  117.      * @throws Exception 
  118.      */  
  119.     public void myCommMethod2() throws Exception {  
  120.         System.out.println("线程===" + Thread.currentThread().getId() + "执行myCommMethod2操作开始");  
  121.         for (int i = 0; i <10; i++) {  
  122.              int a  = i*5;  
  123.              System.out.println(a);  
  124.         }  
  125.         System.out.println("线程===" + Thread.currentThread().getId() + "执行myCommMethod2操作结束");  
  126.     }  
  127. }  

参考文章:
[url]
http://www.ibm.com/developerworks/cn/java/j-lo-test-multithread/index.html?ca=drs-
[/url]
[url]
http://groboutils.sourceforge.net/index.html[/url]

猜你喜欢

转载自www.cnblogs.com/jpfss/p/11582618.html